The goal is 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.

1

There are 1 best solutions below

0
Moses Ighedo On

I have tried using other resources available to me and I have been able to rigmarole my way to an answer. Although, my code may not be so clean, as a leaner, I have achieved the goal using the following codes:

    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},
              'veggiesType': {'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)
    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)
    milkPrice = prices['milkType'].get(milk, 0)
    selectedMenu.append(milkPrice)

    bread = pyip.inputMenu(['wheat_bread', 'white_bread', 'sourdough', 
    'coconut_bread', 
    'buttered_bread'],
                           prompt='What kind of Bread do you want?\n', 
    numbered=True)
    breadPrice = prices['breadType'].get(bread, 0)
    selectedMenu.append(breadPrice)

    protein = pyip.inputMenu(['chicken', 'turkey', 'beef', 'ham', 'tofu'],
                             prompt='What type of protein do you want?\n', 
    numbered=True)
    proteinPrice = prices['proteinType'].get(protein, 0)
    selectedMenu.append(proteinPrice)

    cheese = pyip.inputYesNo(prompt='Type "Yes" or "No" if you want Cheese?')
    if cheese == 'Yes'.lower() or cheese == 'Y'.lower():
        cheeseType = pyip.inputMenu(
            ['swiss', 'shawarma', 'mozerrella', 'cheddar'], numbered=True)
        cheesePrice = prices['cheeseType'].get(cheese, 0)
        selectedMenu.append(cheesePrice)
    else:
        print('Why don\'t you add cheese?')

    veggies = pyip.inputYesNo(
        prompt='Type "Yes" or "No" if you want any of mayo, mustard, lettuce or 
    tomato?')
    if veggies == 'Yes'.lower() or veggies == 'y'.lower():
        veggies = pyip.inputMenu(
            ['mayo', 'mustard', 'lettuce', 'tomato'], numbered=True)
        veggiesPrice = prices['veggiesType'].get(veggies, 0)
    else:
        print('Veggies is good for everyone. Maybe next time')

    sandwichQty = pyip.inputInt(
       prompt='How many sandwiches do you want?\n', blank=False, greaterThan=0)

    print('Summary of your selections:')
    print(f' - {tea} is -----$' + str(prices['teaType'].get(tea, 0)))
    print(f' - {milk} is ----$' + str(prices['milkType'].get(milk, 0)))
    print(f' - {bread} is ---$' + str(prices['breadType'].get(bread, 0)))
    print(f' - {protein} is -$' + str(prices['proteinType'].get(protein, 0)))
    print(f' - {cheese} is --$' + str(prices['cheeseType'].get(cheese, 0)))
    print(f' - {veggies} is -$' + str(prices['veggiesType'].get(veggies, 0)))

    print('')
    print('Your Total Cost is: ', (sum(selectedMenu) * sandwichQty), 'Dollars')