简体   繁体   中英

getting the second key value of dict

I would like to get the second value of my dict and multiply it to quantity is there any way?

menuVariable =  chowMenu = {'a': {'Fried Chicken': 99.99}, 'b': {'Chao Fan': 60.99}, 'c': {'Siomai': 45.99}, 'd': {'Chop Suey': 30.00}}
print(" ******** Welcome  to Chowpanda!.********")

def menu(userInput):
    return chowMenu.get(userInput,"Oops! it seems that is not on the menu")
print('\n'.join("{}: {}\n-----------------------".format(k, v) for k, v in chowMenu.items()))
option = input('Please choose your order:')

while  option != 'a' and option != 'b' and option != 'c' and option != 'd' :
    result = print(menu(option))
    option = input('Please choose your order:')
print(menu(option))
quantity = int(input('How many would you like to order?: '))

Change your nested dicts so they have consistent keys, not a different key in each dict. Then you can use result['price'] to get the price.

You also have other logic problems in the way you print your prompts and call menu() .

menuVariable = chowMenu = {
    'a': {'name': 'Fried Chicken', 'price': 99.99},
    'b': {'name': 'Chao Fan', 'price': 60.99},
    'c': {'name': 'Siomai', 'price': 45.99},
    'd': {'name': 'Chop Suey', 'price': 30.00}
}
print(" ******** Welcome  to Chowpanda!.********")

def menu(userInput):
    return chowMenu.get(userInput,"Oops! it seems that is not on the menu")

print('\n'.join("{}: {}\n-----------------------".format(k, v['name']) for k, v in chowMenu.items()))
while True:
    option = input('Please choose your order:')
    result = menu(option)
    if result != "Oops! it seems that is not on the menu":
        break
    print(result)

price = result['price']
quantity = int(input('How many would you like to order?: '))
total_price = price * quantity
print(f"Total price is {total_price}")

Like this?

chicken = menuVariable['a']['Fried Chicken']
newquantity = chicken * 3
print(newquantity)

Why do you want to nest your dict like that? Would it not be more efficient to use the following dict?

menuVariable1 = {'Fried Chicken': 99.99, 
                'Chao Fan': 60.99, 
                'Siomai': 45.99,
                'Chop Suey': 30.00}

or alternatively:

menuVariable2 =  {'Appetizers' : {'Salad': 29.99, 'Dumplings': 15.99}
                 'Main Courses' : { 'Fried Chicken': 99.99,
                                    'Chao Fan': 60.99, 
                                    'Siomai': 45.99,
                                    'Chop Suey': 30.00}
                 'Desserts' : {'Ice cream' : 30.00}}

Either way, you can use the.get-function to add/multiply values in dicts:

menuVariable1.get('Siomai', 0) * 3

menuVariable2['Main Courses'].get('Siomai, 0) * 3

Both methods give you the multiple of 3 on Siomai

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM