user12739323
user12739323

Reputation:

How to loop through lists and dictionaries

I have a task where I have to loop through a list and then dictionaries to display the total stock worth of a cafe. I created some code that was reviewed but was told these comments:

Try looping through the menu list.

Each item can be used as keys in the dictionaries, to retrieve matching stock and price values.

Stock worth is the sum of each stock item multiplied by its price.

Im just having some trouble on how I would convert the list items to keys and then add them to the dictionary.

My original code was as follows:

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
total = 0
stock = {1: 25,
         2: 20,
         3: 18,
         4: 10
         }

price = {1: 40,
         2: 35,
         3: 28,
         4: 18
         }

for stock in price:
    total = total + price[stock]
total = float(total)
print("The total stock worth is R" + (str(total)))

Thank you for any help and advice!

Upvotes: 2

Views: 1037

Answers (3)

Swati Srivastava
Swati Srivastava

Reputation: 1157

What you have been asked to do is this

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
stock = {'cheeseburger' : 25, 'chicken nuggets' : 20, 'fish' : 18, 'chips' : 10}
price = {'cheeseburger' : 40, 'chicken nuggets' : 35, 'fish' : 28, 'chips' : 18}
total = 0
for i in menu:
    total += stock[i] * price[i]

The value of total, after the for loop is 2384, which is the required answer

Upvotes: 0

Prashant Kumar
Prashant Kumar

Reputation: 2092

Lets go point by point -

1. Try looping through the menu list.

---> Will do this. You will understand why this is needed.

2. Each item can be used as keys in the dictionaries, to retrieve matching stock and price values.

---> You should use the items present in menu list as keys in dictionaries you have created for stock and price instead of using numbers for keys.

This helps to visualize and understand the code and variable structure more clearly.

So you should change it to :-

stock = {'cheeseburger': 25, 'chicken nuggets': 20, 'fish': 18, 'chips': 10}

price = {'cheeseburger': 40, 'chicken nuggets': 35, 'fish': 28, 'chips': 18}

3. Stock worth is the sum of each stock item multiplied by its price.

---> Now you are calculating the total price of stocks by just adding prices of all the stocks. Comment states that the logic for calculating the total stock price should be sum of stock_units*price_of_one_stock for each item in menu.

For doing this you need to iterate through all the items of the menu. Calculate total stock price for each item in menu and add it to final total value.

Something like this:

total = 0
for item in menu:
    total = total + stock[item]*price[item]
print(total)

Upvotes: 1

tenhjo
tenhjo

Reputation: 4537

Try this:

menu = ['cheeseburger', 'chicken nuggets', 'fish', 'chips']
stock = {'cheeseburger': 25,
         'chicken nuggets': 20,
         'fish': 18,
         'chips': 10
         }

price = {'cheeseburger': 40,
         'chicken nuggets': 35,
         'fish': 28,
         'chips': 18
         }


total = 0
for food in menu:
    total += stock[food] * price[food]

print(total)

Upvotes: 2

Related Questions