-3
votes

I would like to Loop through these dictionaries in order to figure out how much money you would make if you sold all of the food in stock. Print that value into the console!

prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 }

stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } all_total =

2
Have you tried anything yet? Where is your code? - Izkata
Easy: print "all_total=", 117.0 - val
@DSM Wow, exact duplicate, even all numbers. Googling for 'banana apple orange pear python' turns up many more. Is this is some standard textbook question or so? - Bas Swinckels

2 Answers

1
votes

Like this:

sum(prices[key]*value for key,value in stock.iteritems())
0
votes

As long as the dictionaries have the same keys, you could use:

all_total = 0
for item in prices:
   all_total += prices[item] * stock[item]