Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 12 Online
OpenStudy (anonymous):

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

OpenStudy (turingtest):

if you want a total you should increment value on each iteration

OpenStudy (andrew.m.higgs):

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.

OpenStudy (anonymous):

value += int(prices[items]) * int(stock[items]) and put the print statement out of the for loop!

OpenStudy (anonymous):

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

OpenStudy (anonymous):

set value to 0 before the loop and an orange costs 1.5, but you use int(price) - use float(price)

OpenStudy (anonymous):

Thanks snark, that was it.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Arriyanalol: i need help
2 days ago 7 Replies 1 Medal
Arriyanalol: CONGRATS to me
2 days ago 8 Replies 2 Medals
Legomaster346: What colleges are good that are near Arab Alabama?
1 week ago 5 Replies 0 Medals
Arriyanalol: i need help
1 week ago 6 Replies 2 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!