Looping through a dictionary for calculation

Question:

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}
Asked By: Thorn

||

Answers:

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]
    
print(total_price)

total_price will have the total value of your stock

Answered By: l00mad
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.