8. A Day at the Supermarket


#1

hi, what is wrong with this code? i get the right answer on the right console as it says in instruction
apple
price: 2
stock: 0
None

but also gives error
Oops, try again.
Check what your code prints for orange. It doesn't look quite right!


prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}


for i in stock:
    if i == "apple":
        print (i)
        print ("price: %s" % prices[i])
        print ("stock: %s" % stock[i])

ok solved , no need for " if i == "apple": " for now


#2

The code is right but for the exercise you need use the method str for convert
for key in prices:
print (key)
print ("price: " + str(prices[key]))
print ("stock: " + str(stock[key]))


#3

man i wish the program would have told us that! thanks!


#4

Correct answer
prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}
stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}
for i in prices:
print i
print 'price: %s' % prices[i]
print 'stock: %s' % stock[i]


#5

Ballpin, I agree that your answer is correct, but you have to use the one devpro60221 provided to get the lesson to accept the answer. This is something that Codeacademy needs to look into.


#6

But it did accept my answer.

If you reset the code at the next task. Codeacademy typed it in the same way
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]

So i don't really understand what bug you are talking about?


#7

I've done mine both way sand it still tells me something is wrong with the way orange prints. Any other ideas?

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: " + str(prices[key])
print "Stock: " + str(stock[key])


#8

LOL, this is nut, the problem is with the
print 'prices: %s' % prices[i]
it should only be print 'price: %s' % prices[i]
it is the difference between price and prices that drives everything off


#9

What I just found it is the difference between capital 'P' and lowercase 'p'. Apparently they have it set to be lowercase 'p'. Mine just went through that way.


#10

This was the solution to my problem. @triet.le I changed the first prices to price and it worked. Thanks!


#11

Glad it helps sometimes, codeacademy just went for the little thing


#12

This was my problem, thanks!


#13

Thank you VERY much! Who could think it's more important than proper code =)


#14

I tried everything else and it was just the capitals that got me the problem. Thanks


#15

Why does the 's' in the prices matter. I think this is a bug and is not correct. I repeated the same exercise in Python IDLE and the program executed successfully and the results were correct.