Writing a food ordering program for school, and am running into the issue of TypeError when trying to take the user input to select the menu item, then use that input to refer quantity value to that dictionary.
`
menu = {
1: {"item": "Green Beret Omelette", "price": "$12.99"},
2: {"item": "Get to the Chopped Salad", "price": "$14.99"},
3: {"item": "Pump You Up Protein Shake", "price": "$9.99"},
4: {"item": "I'll Be Baby Back Ribs", "price": "$22.99"},
5: {"item": "Let Off Some Steamed Vegetables", "price": "$4.99"},
6: {"item": "The Ice Cream Cometh", "price": "$15.99"}
}
key = int(input("Please select your item:\n"))
if int(key) < 1 or int(key) > 6:
print("Invalid selection. Please try again. \n")
isOrdering = True
continue
quan = int(input("Enter quantity: \n")), menu.update[key]({"quantity": quan})
The error I'm currently running into is "Exception has occurred: TypeError 'builtin_function_or_method' object is not subscriptable" for the line:
quan = int(input("Enter quantity: \n")), menu.update[key]({"quantity": quan})
I tried the code above based on a previous attempt at asking that a friend posted for me. Threw the same error as I had in my initial attempt at solving.
The expected/intended output/update (whatever you want to call it) is:
menu choice: 1
quantity: 3
dictionary update: 1: {"item": "Green Beret Omelette", "price": "$12.99", "quantity": 3}
CodePudding user response:
Try:
menu = {
1: {"item": "Green Beret Omelette", "price": "$12.99"},
2: {"item": "Get to the Chopped Salad", "price": "$14.99"},
3: {"item": "Pump You Up Protein Shake", "price": "$9.99"},
4: {"item": "I'll Be Baby Back Ribs", "price": "$22.99"},
5: {"item": "Let Off Some Steamed Vegetables", "price": "$4.99"},
6: {"item": "The Ice Cream Cometh", "price": "$15.99"}
}
from pprint import pprint
print(" CHOOSE AN ITEM FROM THE MENU:")
pprint(menu)
print()
key = 0
while int(key) < 1 or int(key) > 6:
key = int(input("Please select your item (1 or 2 up to 6):\n"))
if int(key) < 1 or int(key) > 6:
print("Invalid selection. Please try again. \n")
quan = int(input("Enter quantity: \n"))
menu[key]["quantity"] = quan
pprint(menu)
print()
print(f'You ordered {key}: {menu[key]["item"]}, quantity: {quan}, total price=${quan*float(menu[key]["price"][1:])}')
CodePudding user response:
I think what you are attempting to do is concatenate to your menu dictionary before it even knows what 'quan' is.
trying getting and then resetting it.
menu = {
1: {"item": "Green Beret Omelette", "price": "$12.99"},
2: {"item": "Get to the Chopped Salad", "price": "$14.99"},
3: {"item": "Pump You Up Protein Shake", "price": "$9.99"},
4: {"item": "I'll Be Baby Back Ribs", "price": "$22.99"},
5: {"item": "Let Off Some Steamed Vegetables", "price": "$4.99"},
6: {"item": "The Ice Cream Cometh", "price": "$15.99"}
}
key = int(input("Please select your item:\n"))
if (int(key) < 1 or int(key) > 6):
print("Invalid selection. Please try again. \n")
isOrdering = True
m1 = menu[key]
quan = int(input("Enter quantity: \n"))
q = {"quantity":quan}
m1.update(q)
menu.update({key: m1})
print("dictionary updated:",list(menu.keys())[0],":",menu[key])
============================
Please select your item:
1
Enter quantity:
3
dictionary updated: 1 : {'item': 'Green Beret Omelette', 'price': '$12.99', 'quantity': 3}