# Something of Value

``````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]
``````

I’m not sure how to add the result of prices * stock to total.

`prices` and `stock` are dicts so you’re not going to want to multiply them. Get the relevant values and multiply those. If it’s a banana, then you’ll need to look up the price and stock for that, if it’s the whole stock then iterate through all keys of either dict.

Hi

this seemed t work for me:

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

Is this the right way or is there a better way?

3 Likes

I had the same code. I believe it is a glitch in Codecademy. On my display, the values being printed were not correct when multiplied.

@singha22
Sounds unlikely. Not saying Codecademy is in any way perfect, just that what you suggest isn’t something I’ve seen happen (I’ve been around a while, seen most ways things tend to go wrong).
print statements which say what’s being done step by step should let you sort that out.

Why does this work, but

total += prices[key] * stock[key]

throws out the error

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

?

2 Likes

Hi

Well I am a complete noob so i cant really answer. My advise would be to see what the total prints out when using total += prices[key] * stock[key] and try and see why the program is getting that total.

Again, total noob here that’s my way of debugging anyway

its the sight i guess it does answer when run on IDLE

do you have to indent the “total = total + prices[key]*stock[key]”
and out the “print total” outside the “for loop”

for key in prices:
…total = total + prices[key]*stock[key]
print total

3 Likes

Hello, I would like to just say that there IS a small bug with lesson 8 and the code
for key in prices:
print key
print “price: %s” % prices[key]
print “stock: %s” % stock[key]

will not work for everyone as it didn’t for me. however some people seem to be able to get it right with the exact same code. Im using chrome, don’t know if that is maybe a source of the problem or maybe it is a problem with their internal servers. The code
total = 0

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

``````print total
``````

is indeed correct (at least it works for me). Don’t know if this helps, just thought I’d make an effort to xD.

1 Like

(PS do not use that indenting as I did not put it in python indent format. But i’m sure you guys can indent with out my help xD)

Looks to me like you were using code meant for another exercise.

check you code code, the compiler is looking for price not prices

Hi there, not sure if this any help but i just use the below codes and it works.
I’m still in the process of learning btw

total = 0
for number in prices :
total = total + prices[number]*stock[number]

print total

Hi there,

You have not saved the value into the `total` variable.

I have written the code like this

``````for fruit in prices:
total += prices[fruit] * stock[fruit]
print total
``````

hope it works.

Thanks

3 Likes

thank you!
I put ‘total = total+price[key]*stock[key]’ inside the for loop
and then
print total
at the end

Because its not a function. Generally use return when in function but since this isn’t, print works fine.

I made a variable inside the for loop called current:

for key in prices:
current = prices[key] * stock[key]

Then I can use my current variable to add that price to the total:

for key in prices:
current = prices[key] * stock[key]
print current
total += current

total += current is the same thing as writing total = total + current

because the orange price is 1.5, %s will return a int so the orange will change to 1 and the answer is incorrect . Try %f ,the answer should be correct