8/13 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 fruits in prices:
 print fruits
 print "price: %s" % prices[key]
 print "stock: %s" % stock[key]

I don’t get my error? Python returns:
orange
price: 2
stock: 0
pear
price: 2
stock: 0
banana
price: 2
stock: 0
apple
price: 2
stock: 0

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


#2

i do get an error:

orange
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?


#3

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


#4

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