Something of value - Don't understand, please help!


#1

How do you do it? I have 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 x in prices:
total = prices[x] * stock[x]
print total

But it says I'm not printing it correctly. Please help!


#2

Does it also say in what way it isn't correct?
Things aren't just "wrong", there is some difference between the current state and what's considered correct. This difference says a great deal about what needs changing. So what's the difference?


#3

Hi in the instruction they aske you to loop throught price and then made that operation
prices[x] * stock[x] and then add it to total so it should be like that

total = 0
for x in prices: 
    n = prices[x] * stock[x]
    total += n
    print total

I stored the a operation inside n and then added it to total


#4

Thank-you for replies, I have got it now :slightly_smiling:


#5

im sorry, i dont understand the total += n syntaxis, could you please explain it? thanks


#6

HI you can do it like that

totale = totale + n
totale = totale + b

Its the same as..

totale += n
totale += b

Its another way to add.


#7

ohhhhhh i get it now, thanks a lot!