Why should i use %s instead of %d for printing the prices from dictionaries?


prices = {}

prices["banana"] = 4
prices['apple'] = 2
prices['orange'] = 1.5
prices['pear'] = 3

stock = {"banana":6, "apple":0, "orange":32, "pear":15}

for key in prices:
    print key
    print "price: %s" %prices[key]
    print "stock: %s" % stock[key]


%d is an integer, for starters. It would not be accurate for prices. %s converts whatever it is given to a string representation.


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