Python餐厅点餐程序问题:如何查看订单并计算总账单?
问题描述
我是Python初学者,这是我的第3个程序,正在学习函数。程序需实现用户先从主菜单(1.开胃菜、2.主菜、3.饮品、4.甜点、5.查看订单、6.退出)选择,每个选项包含子选择(例如开胃菜包含沙拉、薯片等),用户选完商品后可返回主菜单。目前我卡在「查看订单」功能上,需要实现显示用户订单中的每个商品及对应价格,最后展示总账单。请问如何查看用户的所有订单?如何计算总金额?是否需要使用循环?
现有代码
def menu(): print("--MCQUAN MENU--") print("1. Appetizers") print("2. Mains") print("3. Drinks") print("4. Desserts") print("5. View order") print("6. Exit") print() def user_option(): choice = int(input("Choose from the menu & enter a number 1-6: ")) if choice == 1: print() print("APP - heres the apps") print("Salad - $5") print("Chips & salsa - $6") print("Soup - $10") elif choice == 2: print("MAINS - here the mains") print("Pasta - $13") print("Burger - $12") print("Pizza - $10") elif choice == 3: print("DRINKS - heres drinks") print("Water - $0") print("Tea - $3") print("Sprite - $2") elif choice == 4: print("DESSERTS - heres desserts") print("Cake - $6") print("Ice cream - $5 ") print("Pie - $7") elif choice == 5: print("VIEW ORDER -- heres ur order so far") elif choice == 6: print("Exiting") else: print("Invalid") choice = int(input("Enter number 1-6")) def app(): cost = 0 total = 0 user_food = input("Choose which u want ") if user_food == "salad": cheese = input("Cheese? (+$0.50) yes/no ") dressing = input("Dressing? (+$1) yes/no ") elif user_food == "chips & salsa": spicy = input("Spicy? yes/no ") cilantro = input("Cilantro? yes/no ") elif user_food == "soup": soup = input("Chicken noodle or mushroom? ") else: print("Alright!") user_option() user_food = input("Choose which u want ") def main(): user_food = input("Choose which u want ") if user_food == "pasta": cheese_2 = input("Cheese? yes/no ") chilli_f = input("Chilli flakes? yes/no ") elif user_food == "burger": onions = input("Onions? yes/no ") pickles = input("Pickles? yes/no ") elif user_food == "Pizza": pizza = input("Cheese or pepperoni? ") else: print("Alright!") user_option() user_food = input("Choose which u want ") def drinks(): user_food = input("Choose which u want ") if user_food == "water": size = input("Small, medium or large? ") user_option() user_food = input("Choose which u want ") def desserts(): user_food = input("Choose which you want") if user_food == "cake": w_c = input("Whipped cream? yes/no ") cherry = input("Cherry? yes/no ") elif user_food == "ice cream": w_c = input("Whipped cream? yes/no") cherry = input("Cherry? yes/no ") elif user_food == "pie": w_c = input("Whipped cream? yes/no") cherry = input("Cherry? yes/no ") else: print("Alright") user_option() user_food = input("Choose which u want ") #main menu() print("Welcome to McQuans!") print() user_option() app() main() drinks() desserts()
解决方案
核心问题解答
- 如何查看所有订单:用全局列表存储每个订单项(包含商品名称和价格),每次用户选完商品后将信息添加到列表,查看时遍历列表逐个打印。
- 如何计算总金额:维护全局总金额变量,每次添加商品时累加对应价格;或查看订单时遍历订单列表,把所有项的价格相加。
- 是否需要循环:需要。主程序要用循环反复显示菜单,让用户可多次操作直到退出;查看订单时也需要循环遍历订单列表打印商品。
修改后的完整代码
# 全局变量存储订单和总金额 orders = [] total_amount = 0.0 def menu(): print("--MCQUAN 菜单--") print("1. 开胃菜") print("2. 主菜") print("3. 饮品") print("4. 甜点") print("5. 查看订单") print("6. 退出") print() def view_order(): print("\n--- 你的订单 ---") if not orders: print("订单为空") return # 循环遍历订单列表,打印每个商品 for idx, item in enumerate(orders, start=1): print(f"{idx}. {item['name']} - ${item['price']:.2f}") # 打印总金额 print(f"\n总账单: ${total_amount:.2f}") print("---------------\n") def handle_appetizers(): print("\n--- 开胃菜选项 ---") print("沙拉 - $5") print("薯片配莎莎酱 - $6") print("汤 - $10") choice = input("请选择你想要的开胃菜: ").strip().lower() item_name = "" item_price = 0.0 if choice == "沙拉": item_name = "沙拉" item_price = 5.0 cheese = input("加芝士? (+$0.50) 是/否: ").strip().lower() if cheese == "是": item_name += "(加芝士)" item_price += 0.5 dressing = input("加调味酱? (+$1) 是/否: ").strip().lower() if dressing == "是": item_name += "(加调味酱)" item_price += 1.0 elif choice == "薯片配莎莎酱": item_name = "薯片配莎莎酱" item_price = 6.0 spicy = input("加辣? 是/否: ").strip().lower() if spicy == "是": item_name += "(辣)" cilantro = input("加香菜? 是/否: ").strip().lower() if cilantro == "是": item_name += "(加香菜)" elif choice == "汤": soup_type = input("鸡肉面汤还是蘑菇汤? ").strip() item_name = f"{soup_type}汤" item_price = 10.0 else: print("无效选择,返回主菜单") return # 添加到订单 orders.append({"name": item_name, "price": item_price}) global total_amount total_amount += item_price print(f"已添加 {item_name} 到订单!\n") def handle_mains(): print("\n--- 主菜选项 ---") print("意面 - $13") print("汉堡 - $12") print("披萨 - $10") choice = input("请选择你想要的主菜: ").strip().lower() item_name = "" item_price = 0.0 if choice == "意面": item_name = "意面" item_price = 13.0 cheese = input("加芝士? 是/否: ").strip().lower() if cheese == "是": item_name += "(加芝士)" chilli = input("加辣椒碎? 是/否: ").strip().lower() if chilli == "是": item_name += "(加辣椒碎)" elif choice == "汉堡": item_name = "汉堡" item_price = 12.0 onions = input("加洋葱? 是/否: ").strip().lower() if onions == "是": item_name += "(加洋葱)" pickles = input("加酸黄瓜? 是/否: ").strip().lower() if pickles == "是": item_name += "(加酸黄瓜)" elif choice == "披萨": pizza_type = input("芝士披萨还是辣香肠披萨? ").strip() item_name = f"{pizza_type}披萨" item_price = 10.0 else: print("无效选择,返回主菜单") return orders.append({"name": item_name, "price": item_price}) global total_amount total_amount += item_price print(f"已添加 {item_name} 到订单!\n") def handle_drinks(): print("\n--- 饮品选项 ---") print("水 - $0") print("茶 - $3") print("雪碧 - $2") choice = input("请选择你想要的饮品: ").strip().lower() item_name = "" item_price = 0.0 if choice == "水": size = input("小杯、中杯还是大杯? ").strip() item_name = f"{size}水" item_price = 0.0 elif choice == "茶": item_name = "茶" item_price = 3.0 elif choice == "雪碧": item_name = "雪碧" item_price = 2.0 else: print("无效选择,返回主菜单") return orders.append({"name": item_name, "price": item_price}) global total_amount total_amount += item_price print(f"已添加 {item_name} 到订单!\n") def handle_desserts(): print("\n--- 甜点选项 ---") print("蛋糕 - $6") print("冰淇淋 - $5") print("派 - $7") choice = input("请选择你想要的甜点: ").strip().lower() item_name = "" item_price = 0.0 if choice == "蛋糕": item_name = "蛋糕" item_price = 6.0 cream = input("加鲜奶油? 是/否: ").strip().lower() if cream == "是": item_name += "(加鲜奶油)" cherry = input("加樱桃? 是/否: ").strip().lower() if cherry == "是": item_name += "(加樱桃)" elif choice == "冰淇淋": item_name = "冰淇淋" item_price = 5.0 cream = input("加鲜奶油? 是/否: ").strip().lower() if cream == "是": item_name += "(加鲜奶油)" cherry = input("加樱桃? 是/否: ").strip().lower() if cherry == "是": item_name += "(加樱桃)" elif choice == "派": item_name = "派" item_price = 7.0 cream = input("加鲜奶油? 是/否: ").strip().lower() if cream == "是": item_name += "(加鲜奶油)" cherry = input("加樱桃? 是/否: ").strip().lower() if cherry == "是": item_name += "(加樱桃)" else: print("无效选择,返回主菜单") return orders.append({"name": item_name, "price": item_price}) global total_amount total_amount += item_price print(f"已添加 {item_name} 到订单!\n") def main_loop(): while True: menu() try: choice = int(input("请选择菜单编号(1-6): ")) except ValueError: print("请输入有效的数字!\n") continue if choice == 1: handle_appetizers() elif choice == 2: handle_mains() elif choice == 3: handle_drinks() elif choice == 4: handle_desserts() elif choice == 5: view_order() elif choice == 6: print("退出程序,感谢光临!") break else: print("无效编号,请输入1-6之间的数字!\n") # 启动程序 print("欢迎来到McQuans餐厅!") main_loop()
关键修改说明
- 新增全局变量
orders(存储所有订单项)和total_amount(存储总金额),实现跨函数共享订单数据。 - 拆分原函数,每个品类单独做处理函数(如
handle_appetizers),负责展示选项、收集定制需求、计算价格并添加到订单。 - 新增
view_order函数,通过for循环遍历订单列表打印商品信息,最后展示总金额。 - 主程序改为
main_loop函数,用while True循环实现反复显示菜单,直到用户选择退出。
内容的提问来源于stack exchange,提问作者coballt27
相关产品推荐
相关产品推荐

