Supermarket, #8 No error but says my pears don't look right!


#1

There's no error coming up in the console, print in console looks like how the directions show, except the program is telling me "Oops, try again. Check what your code prints for pears. It doesn't look quite right!"
Any ideas??? What's wrong with my pears?? It looks just like the example in the directions! Except for the decimal place in prices, but i tried it without them anyways and still said it was wrong and anyways if you don't add the "%.1f" then the oranges only come up as $1 instead of $1.50

console

orange
price: 1.5
stock: 32
pear
price: 3.0
stock: 15
banana
price: 4.0
stock: 6
apple
price: 2.0
stock: 0
None

code
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: %.1f" % prices[key]
    print "stock: %d" % stock[key]


#2

Double check the code:

    print "price: %.1f" % prices[key]
    print "stock: %d" % stock[key]

There are portions there that are incorrect. For instance, what does %.1f do that could be giving the incorrect result?


#3

It lets the prices go over a decimal point doesnt it? I had %d down first but it was still saying something was wrong. Then i noticed that orange was coming up as 1 dollar instead of 1.5, so i changed it to %.1f because i thought that was the issue, but still saying its wrong

Even with %d, the program is still saying it doesnt look right.

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

orange
price: 1
stock: 32
pear
price: 3
stock: 15
banana
price: 4
stock: 6
apple
price: 2
stock: 0
None
Oops, try again. Check what your code prints for orange. It doesn't look quite right!

Just don't get it? The console doesnt say any errors or anything. I thought it looks just like the example too. I dont know what im missing here


#4

What if you swapped %d for %s?

The problem with using %d is that it is a placeholder for an integer, which by definition has no decimal point. So it can't really be used in this instance.


#5

ooohh, that's why orange was coming up 1 dollar!! wow man, thanks a lot, this has been driving me crazy for a long time THANK YOU!


#6

but just out of curiosity, why would it be %s and not %.1f ?


#7

The .1f would limit it to the tenths, you would need .2f to get to the hundredths. However that is not as reliable as using the round() function, which isn't taught here but may come in handy in the future.


#8

good to know, thanks again!


#9

converts the float value in first to a string


#10

I used %s but still showing me the same error

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

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

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


#11

me to!!! help please!!!


#12

The same for me!
Error:
"Oops, try again. Check what your code prints for orange. It doesn't look quite right!"


#13

Help please
What is wrong here?


#14

Hi, Look carefully , there should be gap between colon and %s


#15

it works now. thanks


#16

My code was this:

Now it works!
Thank you


#17

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