Total worth of inventory


#1



A Day at Supermarket


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


This code yield cost of each commodity w.r.t available stock. But the result pertaining to total worth of the inventory is coming up as 0. Can you guys help me with a fix


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

for name in stock:
    print name
    print "price: %s" % prices[name]
    print "stock: %s" % stock[name]
    total = 0
    value =  prices[name]*stock[name]
    final = value + total
    print "total cost of %s is: %s" % (name,value)
    print final


#2

A few things:
1) total is pointless, it is 0 and is not changed throughout.
2) final is overwritten with each loop with the current stock value.
3) It is finishing on 0 because ( at a guess) it is finishing on apple and so the last value given to final. Dictionaries do not maintain order, they are hashed.

I won't give you the answer but think about points 1 and 2 (point 2 to be honest).


#3

wowowww.....thanks for the hint.

i just moved //total = 0// outside the loop. thats it.


#4

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