I got the output right but it is showing that my code is wrong


prices = {'banana':4, 'apple': 2, 'orange': 1.5, 'pear': 3}
stock = {'banana': 6, 'apple': 0, 'orange': 32, 'pear': 15}
for fruit in prices:
    print fruit
    print 'price: %s' %prices[fruit] 
    print 'stock: %s\n' %stock[fruit]

What is wrong with this code?
And also I'm not getting the output in order ie starting from bananas, instead it is starting with oranges


why do you have a new line character (\n) in your code, no need (remove it). The prints will be on a new line.


Is my code right?
In the output I want to give gap between the fruits. So, I used \n.


but you can't use it, since then the output isn't an exact match. This exercise is really strict


Hi @sunil19 ,

Dictionaries, unlike lists, are unordered. Therefore, when you iterate through the items in a dictionary, they might not be retrieved in the same order in which they were placed into the dictionary.


Oh! Thank you very much!


Okay. Thank you very much! I got it now....


I have another doubt. Why are we writing %s for integer values in the print statement?

print "price: %s" % prices[key]

Here prices[key] value is integer, right?


they are integers indeed, you could use %i as well, not sure why %s was choosen for here, @appylpye, ideas?


For the int type, we would use %d, and for the float type, it would be %f. But this dictionary has a float price for 'orange' and int type for the others ...

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

Therefore, we need to be able to accommodate both. The author of the exercise chose to accomplish this by using %s, which outputs the default str representation of whatever data type is given to it. The submission correctness test (SCT) for this exercise is based on that.

Alternatively, the author could have used %d, but that would truncate the float value for orange. Another possibility would have been to use %f to convert to float. You can experiment with these, if you want, and then restore the %s solution afterwards.

For a real-world solution, it would probably be best to use a float to represent all prices, since monetary systems in general allow fractional parts of the standard denomination. Then, we could use the %f format specifier to indicate how many decimal places of output we want, for example ...

for key in prices:
    print key
    print 'price: $%0.2f' % (prices[key])
    print 'stock: %d ' % (stock[key])

Output ...

price: $1.50
stock: 32 
price: $3.00
stock: 15 
price: $4.00
stock: 6 
price: $2.00
stock: 0

Currently, use of the format method instead of str interpolation is encouraged. Here it is, though it is not the solution to use for this exercise ...

for key in prices:
    print "{:s}".format((key))
    print 'price: ${:0.2f}'.format((prices[key]))
    print 'stock: {:d}'.format((stock[key]))

See 7.1.3. Format String Syntax.

For a look into the future of string formatting, in the yet-to-be-finalized Python 3.6, see PEP 498: Formatted string literals.