这是我正在做的 Python 编码作业,但我想在项目中做一些额外的事情。我仍处于项目的开始阶段,但我不知道如何打印它。
MENU = {
"espresso": {
"ingredients": {
"water": 50,
"coffee": 18,
},
"cost": 1.5,
},
"latte": {
"ingredients": {
"water": 200,
"milk": 150,
"coffee": 24,
},
"cost": 2.5,
},
"cappuccino": {
"ingredients": {
"water": 250,
"milk": 100,
"coffee": 24,
},
"cost": 3.0,
}
}
resources = {
"water": 300,
"milk": 200,
"coffee": 100,
}
def coffee_selection():
selection = {MENU[coffee_choice]}
print(f"A {coffee_choice} costs ${selection['cost']}")
coffee_choice = input("What would you like? (espresso/latte/cappuccino): ")
coffee_selection()
if coffee_choice == "report":
print(f"Water: {resources['water']}ml")
print(f"Milk: {resources['milk']}ml")
print(f"Coffee: {resources['coffee']}ml")
理想情况下,对于用户输入的任何输入,我希望程序在用户选择他们想喝的咖啡类型时打印“A {coffee_choice} cost {上面字典中相应的咖啡选择价格}”。
这是我尝试过的代码,但返回时出现 TypeError: unhashable type: 'dict'
def coffee_selection():
selection = {MENU[coffee_choice]}
print(f"A {coffee_choice} costs ${selection['cost ']}")
coffee_choice = input("What would you like? (espresso/latte/cappuccino): ")
coffee_selection()
if coffee_choice == "report":
print(f"Water: {resources['water']}ml")
print(f"Milk: {resources['milk']}ml")
pint(f"Coffee: {resources['coffee']}ml")