如何将"cost"参数从"MENU"内部移动到单独的 python 文件?


drink = input("Would you like to buy a espresso, latte or cappuccino?:n").lower()
if drink == "espresso":
print(MENU["cost"])

当我尝试输入某些内容时,会出现此错误。有人能帮忙吗?

文件";C: \Users\joeca\Udemy\Games\Day10\咖啡机.py";,第25行,购买中打印(MENU["成本"](KeyError:"cost">

我希望我能把这个"成本"论点移到一个单独的文件中,以便轻松查看和修改。

这个MENU["cost"]意味着MENU是一个字典,并且您试图从该字典中获取'cost'密钥的值
错误显示在字典MENU中没有这样的具有'cost'密钥的密钥/值对

通常,您可以这样做:

from othermodule import othervariable
drink = input("...").lower()
if drink == "espresso":
print(MENU[othervariable])

最新更新