8/13 Keeping Track of the Produce

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

stock = {"banana": 6, "apple": 0, "orange": 32, "pear": 15}
for fruits in prices:
 print fruits
 print "price: %s" % prices[key]
 print "stock: %s" % stock[key]

I don’t get my error? Python returns:
price: 2
stock: 0
price: 2
stock: 0
price: 2
stock: 0
price: 2
stock: 0

And it says Check what your code prints for orange. It doesn’t look quite right!


i do get an error:

Traceback (most recent call last):
File “script.py”, line 6, in
print “price: %s” % prices[key]
NameError: name ‘key’ is not defined

which makes sense, given key is undefined. Which variable holds the value we want to use as key?


i find the trick is to put for key in prices then say print key first and it works


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