Keeping Track of the Produce


Well,i did this lesson and my code was right and run,but i dindnt understand the loop part in the example that they gave us and consequently in my code. I dont get the loop part ''for food in prices:'' that loop is just for the dictionary prices, not for stock. So why the part ''print "stock: %s" % stock[food]'' works and its there ,if the loop its just for prices. Shouldnt be '' for food in prices and stock:'' ,or a separated loop one for prices and other for stock ?

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

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

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


The reason it works for both dictionaries is because they have the same keys. Which dictionary we extract the keys from is of no concern.

Mind, if we wish our reader to put the two together, Python lets us specify both...

for key in prices, stock:


hi i wrote a code like the code above but it doesn't work
please help meUploading...
thank you


for key in prices:
    print key
    print 'prices:%s' % prices[key]
    print 'stock:%s' % stock[key]

Oops, try again. Check what your code prints for orange. It doesn't look quite right!


Be sure there is a space character after the colon.