Why isn't this working? A Day at the Supermaket


#1



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


Code looks right, and prints everything fine, why do I keep getting this error message?
''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 x in prices:
    print x
    print "price:%s" % prices[x]
    print "stock:%s" % stock[x]


#2

print "price:%s" % prices[x]
print "stock:%s" % stock[x]

Problem: Formatting problem!

put space between price: and %s and
same for stock: and %s!
I hope it helps! :slight_smile:


#3

Thanks! Can you explain why you need to use %s?
Why is print "price:"+prices[x]
not ok?


#4

Why not!

But its complex than first one.

As at every iteration you're getting a number.
To use + (concatenation operator) you need convert all numbers into strings using str().

Just use this code and test!

print "price: " + str(prices[x])
print "stock: " + str(stock[x])

Do reply if it let you pass your code or not!
But can you notice that we did interpolation (data conversation )
which may lead to more bug if program gets bigger!
I hope it helps :slight_smile:


#5

I did that earlier and it didn't let me pass the code:( But thanks, at least I know I understood the question


#6

The only reason you did not passed last time because of formatting as I mentioned earlier (space between %s and price/stock)!

Youre welcome! :slight_smile:
Keep up the good work! :thumbsup:


#7

ok I have the format with spaces and still gives me the sampe problem ?


#9

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