Keeping Track of the Produce has very silly requirements


My code does what is asked of me, however for some ungodly stupid reason the lesson wants you to convert integers/floats to strings. My solution preserves the numbers and clearly fulfills the requirements.

Instead of forcing people to convert numbers to strings (why would you ever do this anyway?) the lesson should accept my solution because it preserves the formats of the numbers. This is useful because then you can add them together and do other mathematical operations that cannot be done to strings.

prices = {"banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3}
stock = {"banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15}
for item in prices:
    print item
    if type(prices[item]) == float:
        print "price: %.1f" % (prices[item])
    elif type(prices[item]) == int:
        print "price: %d" % (prices[item])
    print "stock: %d" % (stock[item])


Yes, that is what one might do in the real world for efficiency purposes, however in the context of a learning environment it is easiest to use string conversion.


Perhaps, however the example in question does not instruct the user to use a string in the first place, so how would someone figure out they are supposed to return the values as strings?

If you do what the example code says and use %s then the code does not work because it is trying to cast a float as a string.

Either the instructions need to be more clear or the problem needs to accept numerical answers.


I think you're thinking about what it's asking a bit too hard.

All it wants you to do is print the price and stock as a string.

This passes:

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


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