A Day at the supermarket


#1



10. A Day at the supermaket


getting the following error "Oops, try again. Check what your code prints for orange. It doesn't look quite right!"
But, every aspect displays correctly when run.


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

Should all be lower case "price: %s" and "stock: %s".


#3

wow, now i feel like i need a helmet. Thank you for the quick reply... that resolved the issue!


#4

It's easy to miss those small details. It gets to me too.


#5

This is what I wrote and also get that
"Oops, try again. Check what your code prints for orange. It doesn't look quite right!
I tried other iterations (like changing this to %s - which made less sense to me) ...etc.
Cannot get rid of that message !

for item in prices:
print item
print "price: %.1f " % prices[item]
print "stock: %d " % stock[item]

Yet, my results are :
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


#6

Doing %.1f makes all prices floats. But as you can see, some are 4.0 but is supposed to be 4.
Doing %d makes things into integers so 4.5 becomes 4.
Doing %s are for strings but they also represent values as they are so floats are displayed as floats while integers are displayed as integers. Since you have a mixture of floats and integers for prices, doing %s is probably more flexible as 4.0 gets displayed as 4 and 4.5 gets displayed as 4.5.

Stock is fine with %d since you only have integer values.


#7

Thanks,

However, imho, the quantities should be a %d, and, prices should be a %.1f as that best reflects the reality that :
i) you do not stock partial fruit - thus integers
ii) prices do have decimals, in this example only one (to keep it tidy) but an argument could easily be made for 2.

Again, whatever I type - same error. I'd like to just move on if possible - what can you recommend ?
Thanks !

Marc


#8

At the end of the day : here was the version that let me go through ... Zzzz

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


I still believe my version has a slight edge as I could have done math on it .. which is - what it is / not a string !
-> just in case it helps someone else.

Regards


#9

You could've left stock as %d. I only mentioned to change prices to %s. Stock as %d is not a problem.

Yes, prices do have decimals but in this case, it seems like one of them is a float while the others are integers. Codecademy's test cases for string outputs are pretty tight and leave little room for flexibility hence why %.1f cannot pass the tests.


#10

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