Keeping Track of the Produce


#1

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

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

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

for some reason it is printing the following ans 3 times instead of one :
apple
price: 2
stock: 0


#2

In this exercise, we have to print fruit name followed by price of a fruit on next line followed by stock of that fruit on next line.

Turns out that your current code is just printing about one fruit "apple".

take advantage of your loop iterator (key), as at each iteration I'll take key of price (which is nothing but a string aka fruit name ) dictionary,
You can use this key to print about every fruit.

Note:
Make sure that you use same format as mentioned in instruction(I guess it is point 2. )


#3

Thanks a lot, i thought that i just need to print for one fruit only!!!!


#4

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