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



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!''

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


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:


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


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:


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


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:


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


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