A day at the supermarket


#1



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


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


#2

misspelling...
print "prices: %s" %prices[i]

"price: %s" not"prices: %s"

Another thing..
If inside your code you commented out any code that includes print statement , It will give you same error so delete all commented code then try to run it.


#3

It is possible that the code checker is checking spelling even for literals eg. while printing a sub-heading "price" the system checks whether it is "price" as defined in the system. Since it is looking for "price" to be output, "prices" or even "Price" (i.e. an upper case "p") is being considered by the system as WRONG!! The output in the console comes out correctly as expected without any syntax error.


#4

OMG that is the stupidest thing ever..... took me hours to find that out


#5

Hi

type or paste code here
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]

I am getting error "Check what your code prints for orange. It doesn't look quite right!"
My output is:
orange
price: 1.5
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
None
What's the mistake?
I have tried print "price: %d " % prices[key], also.


#6

Well, I used str():

and it worked. But I don't understand, why the initial code is wrong.


#7

Posting working code may lead to cheating(copy paste by others) (edit your working code)
@payalanand
Tho lets find why your last code did not worked?

Notice that space? (i've marked it)
That's your bug!


#8

Oh, thanks @greentreemee :slight_smile:


#9

I've got the same error and I don't know what's wrong with the code. Where did you used str() @payalanand ?


#10

I'm guessing it's because you have %stock[key] instead of % stock[key].


#11

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