Keeping Track of the Produce


#1



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


I keep getting this error message: Check what your code prints for orange. It doesn't look quite right!

When I review what my code prints in the console, I see that the price for orange is printing as 1 instead of 1.5, the value I have stored for "orange" in my prices dictionary. Is something going wrong because 1.5 is a float and not an integer? Please help!


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])


#2

If the SCT is matching code patterns, then two things might be tripping up the check:

%d

There is nothing wrong with this, except the SCT may be expecting %s

(prices[key])

The parens (tuple syntax) are not needed for single values.


#3

@mtf is correct that you should use %s instead of %d.

Using %d, here is the output for orange ...

orange
price: 1
stock: 32

Here it is when %s is used ...

orange
price: 1.5
stock: 32

Outputting a float with %d truncates any fractional component that may exist. It is really best suited for use with int objects.


#4

Admittedly, I was looking at the code pattern, blissfully ignoring that %d symbolizes integers.


#5

Ah, I see. Thank you both for the explanations! Your help is greatly appreciated.


#6

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