Why do we need to use %s in (8) Keeping Track of Produce?


#1



https://www.codecademy.com/en/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096


I believe I put together a good solution for this excercise. Instead of using %s to substitute they prices and stock, I used %.2f and %d for prices and stock respectively, since the former consists of floats and the latter of integers. Why is this solution not accepted? Is there any reason why outputting as a string is superior?

Currently I receive the error "Check what your code prints for orange. It doesn't look quite right!" but the output seems to be correct.


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

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


for key in prices:
    print key
    print "price: %.2f" % prices[key]
    print "stock: %d" % stock[key]


#2

Both methods produce strings, you can't print anything else. Neither is superior, but only one matches what the instructions specified


#3

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