基于Python模拟咖啡机的咖啡制作类以及原料配置,关于投币可以尝试哦进行更改效果会更好!
# 模拟咖啡机
# 初始化咖啡机的配料和硬币总额
ingredients = {
"水": 2500, # 水量
"牛奶": 1000, # 牛奶量
"厚椰乳": 200, # 厚椰乳
"咖啡": 50 # 咖啡量
}
total_coins = 0 # 硬币总额
# 管理员密码,用于添加配料
admin_password = "123456"
# 菜单,包含每种咖啡的配料和售价
menu = {
"生椰拿铁": {"配料": {"水": 100, "厚椰乳": 30, "咖啡": 50}, "售价": 2.50},
"美式": {"配料": {"水": 100, "牛奶": 50, "咖啡": 20}, "售价": 3.00},
"卡布奇诺": {"配料": {"水": 100, "牛奶": 100, "咖啡": 30}, "售价": 3.50}
}
# 关机标记
is_off = False
# 主循环
while not is_off:
# 获取用户输入
choice = input("你要哪种咖啡?\n(1)生椰拿铁\n(2)美式\n(3)卡布奇诺\n(off)关机\n(check)检查配料\n")
# 处理用户选择
if choice == "off":
print("关机")
is_off = True
elif choice == "check":
print(f"剩余水量: {ingredients['水']}ml\n剩余牛奶: {ingredients['牛奶']}ml\n剩余厚椰乳: {ingredients['厚椰乳']}ml\n剩余咖啡: {ingredients['咖啡']}g\n硬币总额: {total_coins}")
elif choice in ["1", "2", "3"]:
# 选择咖啡类型
coffee_type = menu[list(menu.keys())[int(choice) - 1]]
# 获取用户点的咖啡数量
count = int(input("请问您要点几份?"))
# 检查配料是否足够
insufficient_ingredients = []
for ingredient, amount in coffee_type["配料"].items():
if ingredient in ingredients:
if amount * count > ingredients[ingredient]:
insufficient_ingredients.append(ingredient)
if insufficient_ingredients:
print("配料不足:", ", ".join(insufficient_ingredients))
else:
# 提示用户支付
print(f'您点的是{coffee_type},请支付${coffee_type["售价"] * count}')
# 获取用户支付
paid = 0
while paid < coffee_type["售价"] * count:
coin = input("请投币(p/n/d/q),结束投币请按f,退款请按o")
if coin == "p":
paid += 0.10
elif coin == "n":
paid += 0.50
elif coin == "d":
paid += 1
elif coin == "q":
paid += 2
elif coin == "o":
print(f"退还您${paid},欢迎再来")
paid = 0
break
elif coin == "f":
if paid >= coffee_type["售价"] * count:
# 扣除配料
for ingredient, amount in coffee_type["配料"].items():
if ingredient in ingredients:
ingredients[ingredient] -= amount * count
# 更新硬币总额
total_coins += coffee_type["售价"] * count
# 打印收据
print(f'收到${paid},找零${paid - coffee_type["售价"] * count}\n请收好您的{coffee_type}')
break
else:
print(f'您的投币总额为${paid},还差${coffee_type["售价"] * count - paid}')
else:
print("无效的选择,请重新输入。")