Code gives correct output but it still shows an error


#1



https://www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096


The output to the following code is completely correct but it keeps giving an error - "Check what your code prints for orange. It doesn't look quite right!"
And hence it doesn't go to the next lesson. If anyone has successfully completed this exercise please help me figure out how and what to fix in the code.
Thanks for the help.


prices = {"banana" : 4,
           "apple" : 2,
           "orange" : 1.5,
           "pear" : 3}
stock = {"banana" : 6,
         "apple" : 0,
         "orange" : 32,
         "pear" : 15}
for fruit in prices :
    print fruit
    print "price: %f " % prices[fruit]
    print "stock: %f " % stock[fruit]


#2

trailing space


#4

I'm getting the same thing for the code here, which looks the same to me. Whitespace doesn't seem to be an issue?

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]

#5

Ah ok. So the whitespace inside the string was the issue. Delete the whitespace inside the final two strings and it should work ok.


#6

Hi! I have the same problem but I'm not sure I understand which whitespace you're referring to? I can't get it to work


#7

It's the spaces after %s inside both strings on the last two lines.


#8

i don't have any white space and it's giving me the same error.

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 'prices: %s' % prices[key]
    print 'stock: %s' % stock[key]

output

orange
prices: 1.5
stock: 32
pear
prices: 3
stock: 15
banana
prices: 4
stock: 6
apple
prices: 2
stock: 0
None

error

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

it really gets frustrating when i can put the same code into the python interpreter and i don't get any errors.
yet when i put them in here i do.

python 2.7.9


#9

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