10. Shopping at the Market


#1

My code keeps printing The results twice. How should I write it?

It's currently like this:

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

Printing this:

orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
48.0
48.0
45
93.0
24
117.0
0
117.0
None

#2

its fine, the exercise actually ask you to print total outside the loop, then it will only show once, but you get away with printing total inside your loop


#3

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