The value corresponding to key in dictionary is not a string


Keeping Track of the Produce

Oops, try again. Check what your code prints for orange. It doesn't look quite right!

In the way the dictionary price and stock are defined, the values corresponding to the key are not string but integer or float as they are not written in quotes.
Example 'banana' : 4 and not written as 'banana' : '4' , which makes it a number and not a string.
So why doesn't the code below work, when it use %f or %d instead of %s

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


It does work, but it's not what the exercise is looking for when it grades it. When you run it if you look in the console window you see that it does output as expected, just the exercise will say it's wrong because it is looking for you to use %s.


But you can use anything else if it isn't Codecademy?


That is correct, %f tells it to be formatted as a float, %d formats as a int, and %s formats as a string. Those three are not about what type the input is, but what type you want to format it as.


Okay! I get it now. Thanks!


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