简体   繁体   English

如何获取此代码以从单独的文件 python 文件中取出“成本”参数?

[英]How do I get this code to take out the "cost" argument from a separate file python file?

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

I am getting this error when I try to input something.当我尝试输入内容时出现此错误。 Can anyone help?任何人都可以帮忙吗?

File "C:\Users\joeca\Udemy\Games\Day10\Coffee Machine.py", line 25, in purchase print(MENU["cost"]) KeyError: 'cost'文件“C:\Users\joeca\Udemy\Games\Day10\Coffee Machine.py”,第 25 行,购买打印(MENU[“cost”])KeyError:'cost'

this MENU["cost"] mean that MENU is a dictionary and you try to get value of 'cost' key from that dictionaryMENU["cost"]表示MENU是一个字典,您尝试从该字典中获取'cost'键的值
the error shows you that in dictionary MENU there is no such key/value pair with the 'cost' key该错误显示在字典MENU中没有带有'cost'键的键/值对

In general, you would do it this way:一般来说,你会这样做:

from othermodule import othervariable

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM