9. Something of Value


Oops, try again. It looks like your code did not print the correct total.

prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,

for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
total = 0
for key in prices:
    key = prices[key] + stock[key]
    print key
    total = total + key
print total


What have you done to try to fix this? Do you agree with the error message? Are you able to find anything wrong about how your code behaves?


No, it gets worse when I change things


Maybe you didn't think about what you needed to change before doing the change? "I changed things and it got worse" sounds like you just poked at it without much intention about what you were changing

You'll need to start by confirming whether your result is indeed wrong.
And then if it is, start looking at how it got that incorrect result, do that by reading the code or by printing out what it's doing step by step


key = prices[key] + stock[key]
Replace the + with the multiply (*)
It should be like this:
key = prices[key] * stock[key]


Thank you! It works now.. I feel so stupid.. I understood the exercise wrong.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.