Challenge with Pyinputplus

I am working on a project with a goal to create a program that asks the users what ingredients they would like in their sandwich. In order to ask the users what type of bread or milk or other ingredients they would like, I am using inputMenu() function from pyinputplus such that a user will select choices from the list of menus, then prices for the user’s choices will be drawn from a dictionary of prices into an empty list, where I will then compute the total price for the Sandwich.

This is my program so far:

import pyinputplus as pyip

prices = {
'teaType': {'coffee': 7, 'lipton': 5.5, 'toptea': 5, 'bournvita': 6, 'milo': 6.5},
'milkType': {'peak': 11, 'three_crowns': 9.2, 'cowbell':8.7, 'coolcow': 8},
'breadType': {'wheat_bread': 22, 'white_bread': 15, 'coconut_bread': 17, 'sourdough': 20, 'buttered_bread': 18},
'proteinType':{'chicken': 12, 'turkey': 10.5, 'beef': 9.3, 'ham': 13, 'tofu': 8.7},
'cheeseType': {'swiss': 4, 'shawarma': 5, 'mozerrella': 4.3, 'cheddar':3.8},
'butterType': {'mayo': 2.6, 'mustard': 2.4, 'lettuce': 3, 'tomato': 5}
}


selectedMenu = []

tea = pyip.inputMenu(['coffee', 'lipton', 'toptea', 'bournvita', 'milo'],
      prompt='Type a number for the tea you want.\n', numbered=True)
if tea in prices['teaType'].keys():
    selectedMenu.append(prices['teaType'].values()) 

milk = pyip.inputMenu(['peak', 'three_crowns', 'cowbell', 'coolcow'],
     prompt='Select the milk you want with it\n', numbered=True)
if milk in prices['milkType'].keys():
    selectedMenu.append(prices['milkType'].values())

print(selectedMenu)

The challenge is that when I print the selectMenu list, I got the entire list of prices in the dictionary as follows: [dict_values([7, 5.5, 5, 6, 6.5]), dict_values([11, 9.2, 8.7, 8])].

What I want to achieve is to go into the dictionary of prices and pick only the prices for the items that the user has picked into the selectMenu list and then sum up the total price for the user.

I don’t how or what other ways to achieve it. I need help with it.

if tea in prices[‘teaType’].keys():
selectedMenu.append((tea, prices[‘teaType’][tea]))

if milk in prices[‘milkType’].keys():
selectedMenu.append((milk, prices[‘milkType’][milk]))

Thanks a lot. It worked

But I made a little modification. When I implemented your exact code, it gave me a list of tuples which I couldn’t sum. So, I did this:

if tea in prices['teaType'].keys():
    selectedMenu.append((prices['teaType'][tea]))

if milk in prices['milkType'].keys():
    selectedMenu.append((prices['milkType'][milk]))

print(sum(selectedMenu))

With this I was able to get the sum of the selected items as required.

Through my study from other sources, I also found another method that works. This time, its using the get method() where a default value of zero is provided to prevent the program from crashing in case the specified key is not found in the dictionary.

The method goes thus:

import pyinputplus as pyip

selectedMenu = []
tea = pyip.inputMenu(['coffee', 'lipton', 'toptea', 'bournvita', 'milo'],
                     prompt='Type a number for the tea you want.\n', numbered=True)
# Add the price of the selected tea from dictionary of prices above, if no price, add zero.
teaPrice = prices['teaType'].get(tea, 0)
    selectedMenu.append(teaPrice)

milk = pyip.inputMenu(['peak', 'three_crowns', 'cowbell', 'coolcow'],
                      prompt='Select the milk you want with it\n', numbered=True)
# Add the price of the selected milk from dictionary of prices above, if no price, add zero.
milkPrice = prices['milkType'].get(milk, 0)
selectedMenu.append(milkPrice)

print(sum(selectedMenu))