Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

total_price will have the total value of your stock

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading