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


#1



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]


#2

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


#3

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