Something of value


#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
def lucro_total(key):
for key in prices:
a = prices[key] * stock[key]
print a
total = total + a
return total
print total

What's wrong here? why is printing 0 and not the correct total?


#2

I suggest adding print statements where you print out what you're doing step by step.
That way you can determine what parts of your code isn't having the desired effect


#3

You don't need function here. One loop is enough.

total = 0 --> ok
def lucro_total(key): --> delete this
for key in prices: --> ok
a = prices[key] * stock[key] --> it should be total = total + (prices[key] * stock[key])
print a --> delete this
total = total + a --> delete this
return total --> delete this
print total --> print total --> ok, but must be outside the loop


#4

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