How can I move the "cost" argument from inside of 'MENU' to a separate python file?

Question:

    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:UsersjoecaUdemyGamesDay10Coffee Machine.py", line 25, in purchase
print(MENU["cost"])
KeyError: ‘cost’

I was hoping that I could move this ‘cost’ argument to a separate file in order for it to be easily viewed and altered.

Asked By: joeca

||

Answers:

this MENU["cost"] mean that MENU is a dictionary and you try to get value of 'cost' key from that dictionary
the error shows you that in dictionary MENU there is no such key/value pair with the 'cost' key

Answered By: Dmitriy Neledva

In general, you would do it this way:

from othermodule import othervariable

drink = input("...").lower()
if drink == "espresso":
    print(MENU[othervariable])
Answered By: John Gordon
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.