סכום וממוצע
חלק מהיחידה יסודות במסלול ה-Python של Coddy. שיעור 72 מתוך 77.
אתגר
קלטפל באפשרות לחישוב ההוצאה הכוללת והממוצעת (3).
אם רשימת ההוצאות ריקה, הצג:
No expenses recorded yet.אחרת, הצג את הרשימה בפורמט הבא:
Total expense: 600.0
Average expense: 200.0בהנחה שההוצאות שהוזנו קודם לכן היו 300, 200 ו־100. (בסדר הזה!)
נסו בעצמכם
print("Welcome to the Daily Expense Tracker!")
# Display menu once
print("\nMenu:")
print("1. Add a new expense")
print("2. View all expenses")
print("3. Calculate total and average expense")
print("4. Clear all expenses")
print("5. Exit")
# Initialize an empty list to store expenses
expenses = []
while True:
# Get user choice
choice = input()
if choice == "1":
# Add a new expense
amount = float(input())
expenses.append(amount)
print("Expense added successfully!")
elif choice == "2":
# View all expenses
if len(expenses) == 0:
print("No expenses recorded yet.")
else:
print("Your expenses:")
for i in range(len(expenses)):
print(f"{i + 1}. {expenses[i]}")
elif choice == "5":
# Exit the program
print("Exiting the Daily Expense Tracker. Goodbye!")
breakכל השיעורים ביחידה יסודות
11יסודות הרשימות
הכרזה על רשימהגישה לאיברי רשימהשינוי רשימותמתודות של רשימהחזרה — רשימת מוצריםחזרה — רשימה הפוכהTuple14מעקב הוצאות יומי
סקירת הפרויקטיציאה מהתוכניתתרגלו בעצמכם: קומפיילר Python אונליין