Looping through a dictionary for calculation


I’m brand new if you can’t tell.

I want to loop through each dictionary, so that I can calculate the total worth of the ‘menu’ but i have no idea how to construct the loop.

Please help…

menu = ["Cappuccino", "Espresso", "Latte", "Macchiato"]
stock = {"Cappuccino": 24,
         "Espresso": 18,
         "Latte": 39,
         "Macchiato": 43}
price = {"Cappuccino": 4.36,
         "Espresso": 1.70,
         "Latte": 3.25,
         "Macchiato": 1.80}

>Solution :

You need to use the for loop for this.
I assume you want to sum the number of each element multiplied by the nummber ofelements in stock

total_price = 0 

for item in menu:
    total_price += price[item] * stock[item]

total_price will have the total value of your stock

Leave a Reply Cancel reply