9. Something of Value. your code did not print the correct total. (117.0)


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/4?curriculum_id=4f89dab3d788890003000096


Hello, the error message occurs:
Oops, try again. It looks like your code did not print the correct total.


The total I 've found is 117. Not sure why an error message does occur?
Would you plz help?


prices = {
    "banana" : 4,
    "apple"  : 2,
    "orange" : 1.5,
    "pear"   : 3,
}
stock = {
    "banana" : 6,
    "apple"  : 0,
    "orange" : 32,
    "pear"   : 15,
}
total = 0
for key in prices:
    print('\n' + key)
    print("price: %s" % prices[key])
    print("stock: %s" % stock[key])
    print(prices[key]* stock[key])
    total += prices[key]*stock[key]
print("\n TOTAL: %s" % total)


#2

This code worked ............ .....

prices = {
"banana" : 4,
"apple" : 2,
"orange" : 1.5,
"pear" : 3,
}
stock = {
"banana" : 6,
"apple" : 0,
"orange" : 32,
"pear" : 15,
}
total = 0
for key in prices:
print key
total += (prices[key]*stock[key])
print total
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
print total


#3

So I dont understand how either of you arrived at your submissions.


#4

My code:

Summary

total = 0

for key in prices:
    value = prices[key] * stock[key]
    print value
    total += value
print total

#5

just write print total no additional text like "\n TOTAL: %s" %


#6

Thank you,
not sure to understand why
print("\n TOTAL: %s" % total)
didn't work, but print total with no additional text works fine :slight_smile:
Thx!!


#7

This is my code. I created my own variable and later added it to 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:
frank = prices[key]*stock[key]
total=total + frank

print total


#8

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