Xóa tất cả
Thuộc phần Nền tảng cơ bản trong hành trình Python của Coddy. Bài học 73 trên 77.
Thử thách
DễXử lý tùy chọn xóa tất cả chi phí (4).
Sau khi xóa danh sách chi phí, hãy in ra:
All expenses cleared.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 == "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 == "3":
# Calculate total and average expense
if len(expenses) == 0:
print("No expenses recorded yet.")
else:
total = 0
for expense in expenses:
total += expense
average = total / len(expenses)
print(f"Total expense: {total}")
print(f"Average expense: {average}")
elif choice == "5":
# Exit the program
print("Exiting the Daily Expense Tracker. Goodbye!")
breakTất cả bài học trong Nền tảng cơ bản
4Các toán tử, phần 2
Toán tử logic, phần 1Toán tử logic, phần 2Ôn tập: logic đơn giảnToán tử logic, phần 3Toán tử logic, phần 47Máy tính chia hóa đơn
Lời chàoNhận đầu vào8Vòng lặp
Vòng lặp forVòng lặp whileBreakContinueÔn tập: Giai thừaHàm rangeVòng lặp lồng nhauÔn tập: Đầu vào động11Kiến thức cơ bản về danh sách
Khai báo danh sáchTruy cập phần tử trong danh sáchChỉnh sửa danh sáchPhương thức của danh sáchÔn tập: danh sách sản phẩmÔn tập: danh sách đảo ngượcTuple14Trình quản lý chi tiêu hằng ngày
Tổng quan dự ánThoát chương trìnhTự luyện tập: Trình biên dịch Python online