添加支出
本课属于 Coddy Python 学习旅程的“基础知识”单元。第 70 课,共 77 课。
挑战
简单处理用户添加一笔支出的选项(1)。
- 在程序开始时,初始化一个空的 expenses 列表
- 当用户选择
1作为选项时,再获取用户的一次输入,类型为float,并将其值添加到 expenses 列表中。 - 添加后,输出:
Expense added successfully!亲手试试
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")
while True:
# Get user choice
choice = input()
if choice == "5":
# Exit the program
print("Exiting the Daily Expense Tracker. Goodbye!")
break基础知识中的所有课
自己练习: Python 在线编译器