A day at supermarket


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

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

for key in prices:
    #if key == "apple":
        print key
        print "price:"+ str(float(prices[key])) 
        print "stock:"+str(stock[key])


Hi @spddd,

I guess you are experimenting around by not really sticking to the method suggested by the instructions but that is fine.

A few comments regarding your code:

1... the (first) main reason why it is rejected

The instructions specified that you should print out the information regarding prices and stocks according to the following format:

price: 2
stock: 0

This is very sneaky indeed, because upon closer observation, you'll notice that there is a spacing between each term (e.g. price) and the corresponding number (e.g. 2). Your proposed method, which utilises string concatenation, must then take extra care to include this spacing. Hence, instead of:

what you should instead do is:

print "price: " + str(float(prices[key])
print "stock: " + str(stock[key])

Notice that a spacing has been deliberately included in every bit (e.g. "price:(spacing)"). This will allow your code to pass the exercise.

2... the (second) reason why it is rejected

The part where you converted the prices[key] to a float isn't necessary because by doing prices[key] you didn't change it into anything else like an integer etc. So you can just use print "price: " + str(prices[key]). Besides, by converting all the prices to floats, you will get an error message for pear, banana, and apple because their prices were never floats to begin with :stuck_out_tongue:

3... Indentations

It would be nice, I think, if you make extra effort to indent everything super nicely, like removing the extra spacing ahead of the prices dictionary and putting your bananas in the next line. This has no bearing on how your code turns out here though. Just thought it would be nice :stuck_out_tongue:

Hope this helps :slight_smile: