Something of Value


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

How do I add all my totals.

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

for key in prices:
    print prices[key] * stock[key]
print total

total = 0

You've got a good start, an initialized variable to accumulate. You've also got the proper loop construct so we're half the way there.

The code on the next line is correct but for one small detail. It is being sent to the display rather than being added to the total. Make that change and your loop will finish with a total.

There are two ways we can add to total

total = total + ...


total += ...

The ... is your code from the print statement (sans print).


I figured it out before your response but thank you for your quick reply. And also for the second solution.


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