In: Computer Science
I need to write a program in python for a restaurant. The program should let the user enter a meal number, then it should display the meal name, meal price, and meal calories. Also, needs the appropriate output if the user enters an invalid meal number. I am supposed to use a dictionary, but my problem is it keeps giving me an error and telling me my menu is not defined. Not sure what I am doing wrong.
print ("Welcome to INN N OUT, what would you like to order
today?")
print("------Menu------")
print("Double Double - 1")
print("Cheeseburger - 2")
print("Hamburger - 3")
def Menu():
menu = {
1 : {'name' : 'Double Double','price' : 3.45,'calories' : 670},
2 : {'name' : 'Cheeseburger','price' : 2.40,'calories' : 480},
3: {'name' : 'Hamburger','price' : 2.10,'calories': 310}}
x = int(input("Make a selection: "))
if x >= 1 and x <= 3:
print("You ordered a " + menu[x]['name'] + ". That is " +
menu[x]['calories'] + ". Your total will be $" +
menu[x]['price'])
else:
print("Invalid entry.")
Menu()
I hope the error that you were getting was due to the proper indentation in Python. Please note that Python strictly identifies and executes the code based on the indentation. I have indented it properly. Please see the code below. Also please refer to the screenshot for guidance on the indentation.
Another thing that I have noticed is when you are printing the price and calories in the output. It was giving an error when trying to concatenate int and string. An explicit conversion to string is required when concatenating int with string. I have modified that also.
print ("Welcome to INN N OUT, what would you like to order today?")
print("------Menu------")
print("Double Double - 1")
print("Cheeseburger - 2")
print("Hamburger - 3")
def Menu():
menu = {
1 : {'name' : 'Double Double','price' : 3.45,'calories' : 670},
2 : {'name' : 'Cheeseburger','price' : 2.40,'calories' : 480},
3 : {'name' : 'Hamburger','price' : 2.10,'calories': 310}}
x = int(input("Make a selection: "))
print(x)
if x >= 1 and x <= 3:
#When concatenating string and int, use str() function to convert int to string and then concatenate.
print("You ordered a " + menu[x]['name'] + ". That is " + str(menu[x]['calories']) + " calories. Your total will be $" + str(menu[x]['price']))
else:
print("Invalid entry.")
Menu()
The screenshots of the code and output are provided below.