Something of Value


#1

How can we get 'total' adding 'each_total' programattically without having to add them individually as below

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:
    each_total = prices[key]*stock[key]
    print each_total
    
total = 48.0 + 45 + 24 + 0

print total

#2

It's the same as incrementing counters using +=.
For each each_total you calculated, you can increment your total with that in the same loop.
total += each_total.


#4

This topic is solved.