すべての支出を表示
CoddyのPythonジャーニー「Fundamentals」セクションの一部 — レッスン 71/77。
チャレンジ
簡単すべての経費を表示するオプション(2)を処理します。
経費リストが空の場合は、次のように出力します:
No expenses recorded yet.そうでない場合は、次の形式でリストを出力します:
Your expenses:
1. 23.1
2. 35.5
3. 99.99
4. 15.2以前に入力された経費が 23.1、35.5、99.99、15.2 であると仮定します。(この順序で!)
自分で試してみよう
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 == "5":
# Exit the program
print("Exiting the Daily Expense Tracker. Goodbye!")
break