Menu
CoddyTech

Xem tất cả khoản chi

Thuộc phần Nền tảng cơ bản trong hành trình Python của Coddy. Bài học 71 trên 77.

challenge icon

Thử thách

Dễ

Xử lý tùy chọn xem tất cả chi phí (2).

Nếu danh sách chi phí trống, hãy in ra:

No expenses recorded yet.

Nếu không, hãy in danh sách theo định dạng sau:

Your expenses:
1. 23.1
2. 35.5
3. 99.99
4. 15.2

Giả sử các chi phí đã nhập trước đó là 23.1, 35.5, 99.99 và 15.2. (Theo thứ tự đó!)

Tự làm thử

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

Tất cả bài học trong Nền tảng cơ bản

Tự luyện tập: Trình biên dịch Python online