Not sure how to add the sub totals to the total


#1

Something of Value

Not sure how to extract the totals generated and add them together to get the final total output.

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


#2

Rather than print, accumulate. Start by initializing a variable before the loop, then augment inside the loop. You have total already, now just add values to that.


#3

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