Something of value program


#1

Continuing the discussion from 9. Something of Value!:



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:
    prices[key] = prices[key]*stock[key]
    print prices[key]
    total += prices[key]

print 'total value is: %s ' %total

I got the correct total but still it is showing this error- It looks like your code did not print the correct total.
Is there anything wrong in my code?


#2

Try this:
for key in prices:
x = prices[key] * stock[key]
total += x
print total


#3

Muito Obrigado pela ajuda.


#4

above the loop you have to initialize total (data type[int]).


#5

total = 0
for h in prices:
worth = prices[h] * stock[h]
print worth
total += worth
print total

What is that is causing my value for total to come out as zero?