Hi Guys, i am out of ideas... Something of value


#1



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


Hey Guys, i ask humbly for your assistance. in my head it should work. But it doesn't...
It prints out the individual values correctly. But not the total. i even checked with other answers in the forum. I am out of ideas now.

i get this instead:
Oops, try again. It looks like your code did not print the correct total. And true it is. according to my code, the total is zero..


Replace this line with your code. 
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
    total_each_fruit = prices[key]*stock[key]
    print "total each fruit: %s" % total_each_fruit
    total = total + total_each_fruit
    
print total


#2

total should be initialized before the loop, not inside it. Your code is resetting it to zero with each iteration of the loop.

The variable and extra print statement are not necessary. Simply accumulate to your working variable.

total += prices[key] * stock[key]

#3

thank you very much.

i changed it now to this, it still doesn't work..

total = 0
for key in prices:
    print key
    print "price: %s" % prices[key]
    print "stock: %s" % stock[key]
    total_each_fruit = prices[key]*stock[key]
    print "total each fruit: %s" % total_each_fruit
   

total += prices[key]*stock[key]
print total

#4

Suggest rermove those two lines.

This line should be inside the loop block.


#6

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