Keeping Track of the Produce


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


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


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


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


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

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

price: 1
stock: 32

Here it is when %s is used ...

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.


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


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


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