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


#1

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


#2

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


#3

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


#4

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


#5

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.


#6

Oh! Thank you very much!


#7

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


#8

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?


#9

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


#10

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 ...

orange
price: $1.50
stock: 32 
pear
price: $3.00
stock: 15 
banana
price: $4.00
stock: 6 
apple
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.