这是一个简单的基于 Python 命令行的家庭版点菜功能示例代码,实现菜品展示、点菜、查看已点菜品及计算总价等基本操作,大家用起来吧!
# 菜品及价格字典
dishes = {
"宫保鸡丁": 25,
"鱼香肉丝": 22,
"糖醋排骨": 30,
"麻婆豆腐": 18,
"清炒时蔬": 15
}
# 已点菜品列表
ordered_dishes = []
def show_menu():
print("家庭餐厅菜单:")
for dish, price in dishes.items():
print(f"{dish} - ¥{price}")
def take_order():
while True:
dish_name = input("请输入要点的菜品名称(结束点菜请输入'q'):")
if dish_name == 'q':
break
if dish_name in dishes:
ordered_dishes.append(dish_name)
print(f"{dish_name}已加入订单。")
else:
print("该菜品不存在,请重新输入。")
def show_order():
print("已点菜品:")
for dish in ordered_dishes:
print(dish)
def calculate_total():
total = 0
for dish in ordered_dishes:
total += dishes[dish]
return total
if __name__ == "__main__":
show_menu()
take_order()
show_order()
total_price = calculate_total()
print(f"总价:¥{total_price}")
这个程序首先定义了一个菜品字典,包含菜品名称和价格。show_menu函数用于展示菜单,take_order函数让用户输入要点的菜品并添加到已点列表,show_order函数用于显示已点菜品,calculate_total函数计算已点菜品的总价。在主程序部分,依次调用这些函数来实现完整的点菜流程,最后输出总价。

如果希望有更丰富的功能,比如菜品分类、数量选择、优惠计算等,可以进一步扩展代码。例如,添加菜品数量选择功能:
# 菜品及价格字典
dishes = {
"宫保鸡丁": 25,
"鱼香肉丝": 22,
"糖醋排骨": 30,
"麻婆豆腐": 18,
"清炒时蔬": 15
}
# 已点菜品及数量字典
ordered_dishes = {}
def show_menu():
print("家庭餐厅菜单:")
for dish, price in dishes.items():
print(f"{dish} - ¥{price}")
def take_order():
while True:
dish_name = input("请输入要点的菜品名称(结束点菜请输入'q'):")
if dish_name == 'q':
break
if dish_name in dishes:
quantity = int(input(f"请输入{dish_name}的数量:"))
if dish_name in ordered_dishes:
ordered_dishes[dish_name] += quantity
else:
ordered_dishes[dish_name] = quantity
print(f"{quantity}份{dish_name}已加入订单。")
else:
print("该菜品不存在,请重新输入。")
def show_order():
print("已点菜品:")
for dish, quantity in ordered_dishes.items():
print(f"{dish} x {quantity}")
def calculate_total():
total = 0
for dish, quantity in ordered_dishes.items():
total += dishes[dish] * quantity
return total
if __name__ == "__main__":
show_menu()
take_order()
show_order()
total_price = calculate_total()
print(f"总价:¥{total_price}")
这段代码修改了数据结构,使用字典来存储已点菜品及其数量,在点菜函数中增加了数量输入的功能,计算总价时也相应地乘以数量,使得点菜功能更加实用。如果后续要对接数据库存储订单信息或者结合图形界面(如使用 Tkinter、PyQt 等)来呈现更友好的交互,也是基于此基础代码进行拓展。
好啦!大家快去试试吧! 等你们的回复。
2206

被折叠的 条评论
为什么被折叠?



