Reputation: 69
Writing a Python program - scenario is Chinese restaurant, orders are input with integers which correspond to menu items and prices. Can't work out how to link the input numbers to the corresponding menu item and price. Have two lists, one for menu and one for price. User inputs 1 if they want rice, etc. Want to show an order summary which shows the menu item and the price and creates a total for the whole order.
Looked in Python books but can't find how to solve my problem
menu = ['rice', 'egg fried rice', 'prawn crackers']
price = [1.00, 2.00, 1.50]
again = True
while again:
incorrect_menu_item = True
while incorrect_menu_item:
menu_item = int(input('Enter menu item: '))
if menu_item >=1 and menu_item <= len (menu):
incorrect_menu_item = False
print('menu item accepted')
new_order.append(menu_item)
new_order.sort()
else:
print('incorrect input')
another_item = input('Another item? n to stop, enter to continue: ')
if another_item == 'n':
again = False
print('Thank you, order is complete')
Have tried to link the input number to the index of the menu and price lists but it is not working
Upvotes: 1
Views: 258
Reputation: 694
A way you could do this is using an array of dictionaries, a set of key value pairs. It could look a bit like this:
menu = [
{
"index": 59,
"name": "Plain rice",
"price": 1.2
},
{
"index": 33,
"name": "Something else",
"price": 2.5
}
]
You could then query this like so:
for i, item in enumerate(menu):
if item["index"] == menu_item:
# Do something with the correct item
# e.g: item["price"] to get the price
Upvotes: 1