I'm trying to add up the inventory and prices for this exercise, but am having trouble figuring out how to get it to add up. Can someone help? Thx prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } for items in prices: value = int(prices[items]) * int(stock[items]) print value
if you want a total you should increment value on each iteration
Yes, and unless that 'print value' is meant for debugging I would think you probably want to unindent it so that it prints after the for loop.
value += int(prices[items]) * int(stock[items]) and put the print statement out of the for loop!
This is kind of weird. When I'm in Python2.6 it is not working at all. When I am in Codecademy, it comes up with the wrong answer. It should come up with 117, but in Codecademy it is coming up with 101. I'm not quite at the understanding of how to properly do incrementing For Loops. prices = { "banana": 4, "apple": 2, "orange": 1.5, "pear": 3 } stock = { "banana": 6, "apple": 0, "orange": 32, "pear": 15 } for items in prices: value += int(prices[items]) * int(stock[items]) print value Can you tell me where it is not working correctly, or what correct coding should look like? Thanks
set value to 0 before the loop and an orange costs 1.5, but you use int(price) - use float(price)
Thanks snark, that was it.
Join our real-time social learning platform and learn together with your friends!