Help


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

Hi, it shows 0, i don’t know why


#2

you re-assign total each iteration of the loop, so it will only remember the final value. 2 (price apple) * 0 (stock of apple) is zero.

You need to increase total each iteration of the loop, not re-assign it


#3

Ah, i see… How do I do that?


#4

you need to store the current value again the variable you want to update:

x = 5
# re-assigning x
x = 3
# increasing x
x = x + 3

#5

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