咖啡机器模拟器代码问题:交易金额无法同步至resources的money字段
问题修复:咖啡机器模拟器的金额统计与报告显示问题
核心问题分析
- 报告内容未动态更新:
report字符串在程序启动时就固定了初始值,后续resources['money']变化不会同步到报告里,导致输入report始终显示0。 - 金额累加逻辑混乱:
- 浓缩咖啡分支:无论用户付款是否足够,都执行金额累加,退款场景不该加钱。
- 拿铁和卡布奇诺分支:仅在付款超过售价时才累加金额,等于售价的情况遗漏。
- 卡布奇诺分支重复调用
money(),导致用户需两次输入硬币。
- 原料检查逻辑错误:拿铁的原料判断中,水量阈值写错(应该是<200而非<50),部分判断用
<=导致临界值误判。 - 菜单键名不一致:拿铁的原料里写了
coffee_l,和其他饮品及资源里的coffee不匹配,导致原料检查逻辑失效。
修复后的完整代码
menu = { "espresso": { "ingredients": { "water": 50, "coffee": 18, }, "cost": 1.5, }, "latte": { "ingredients": { "water": 200, "milk": 150, "coffee": 24, # 修正键名,去掉coffee_l }, "cost": 2.5, }, "cappuccino": { "ingredients": { "water": 250, "milk": 100, "coffee": 24, }, "cost": 3.0, } } resources = { "water": 300, "milk": 200, "coffee": 100, "money": 0 } def check(drink): req = menu[drink]['ingredients'] # 统一检查逻辑,避免重复代码 for item, amount in req.items(): if resources[item] < amount: return f"Sorry there is not enough {item}." return "enough" def get_payment(): # 重命名函数,避免和money变量混淆 quarter = 0.25 dime = 0.1 nickel = 0.05 pennie = 0.01 qu = float(input("Insert quarters: ")) di = float(input("Insert dimes: ")) ni = float(input("Insert nickels: ")) pen = float(input("Insert pennies: ")) total_paid = qu*quarter + di*dime + ni*nickel + pen*pennie return total_paid def generate_report(): # 动态生成报告,每次调用都读取最新资源状态 return f"Water: {resources['water']}ml\nMilk: {resources['milk']}ml\nCoffee: {resources['coffee']}ml\nMoney: {round(resources['money'],2)}$" while True: print("Welcome to the coffee machine.") m = "Espresso = 1.5$\nLatte = 2.5$\nCappuccino = 3.0$\n" print(m) choice = input("What would you like? (espresso/latte/cappuccino)(e/l/c): ") if choice == 'e': drink = 'espresso' chk = check(drink) print(chk) if chk == "enough": mo = get_payment() cost = menu[drink]['cost'] if mo >= cost: # 扣除原料 for item, amount in menu[drink]['ingredients'].items(): resources[item] -= amount # 累加实收金额(只加饮品售价,不是用户付的全部,因为要找零) resources['money'] += cost change = round(mo - cost, 2) if change > 0: print(f'Here is your coffee, and here is your change {change} $') else: print('Here is your coffee') print(f"Money in machine is: {round(resources['money'],2)}") else: print(f'Sorry that\'s not enough money. Money refunded.') elif choice == 'l': drink = 'latte' chk = check(drink) print(chk) if chk == "enough": mo = get_payment() cost = menu[drink]['cost'] if mo >= cost: for item, amount in menu[drink]['ingredients'].items(): resources[item] -= amount resources['money'] += cost change = round(mo - cost, 2) if change > 0: print(f'Here is your coffee, and here is your change {change} $') else: print('Here is your coffee') print(f"Money in machine is: {round(resources['money'],2)}") else: print(f'Sorry that\'s not enough money. Money refunded.') elif choice == 'c': drink = 'cappuccino' chk = check(drink) print(chk) if chk == "enough": mo = get_payment() # 去掉重复调用 cost = menu[drink]['cost'] if mo >= cost: for item, amount in menu[drink]['ingredients'].items(): resources[item] -= amount resources['money'] += cost change = round(mo - cost, 2) if change > 0: print(f'Here is your coffee, and here is your change {change} $') else: print('Here is your coffee') print(f"Money in machine is: {round(resources['money'],2)}") else: print(f'Sorry that\'s not enough money. Money refunded.') elif choice == 'report': print(generate_report()) # 调用动态生成报告的函数 elif choice == 'off': break
关键修复点说明
- 动态生成报告:用
generate_report()函数替代固定字符串,每次调用都读取最新的resources数据。 - 统一金额累加逻辑:只有用户付款足够时,才把饮品售价(不是用户付的全部,因为找零要退回去)加到机器金额里,同时扣除对应原料。
- 修复重复付款输入:卡布奇诺分支去掉重复的
money()调用。 - 优化原料检查函数:用循环统一检查所有原料,避免重复代码和阈值错误。
- 修正菜单键名:拿铁的原料键名改为
coffee,和资源保持一致。
内容的提问来源于stack exchange,提问作者user21022383
相关产品推荐
相关产品推荐

