Keeping track of the produce: I'm not sure why my code works


#1



/www.codecademy.com/courses/python-beginner-en-IZ9Ra/1/3?curriculum_id=4f89dab3d788890003000096

My code works fine, I'm just not sure why it works

I don't understand why

print produce will print the dictionary keys but

print prices[produce] will print the dictionary values.


prices = {'banana':4, 'apple':2, 'orange': 1.5, 'pear': 3}
stock = { 'banana': 6, 'apple':0, 'orange': 32, 'pear': 15}

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


I too got the same problem
#2

because you can use the dictionary key to access values inside a dictionary?


#3

Well how do I distinguish between accessing the keys or the values?


#4

the variable which hold the key gets created inside the for loop:

for produce in prices:
    print produce

so produce will now hold the key, then you can use dictionary name + key to access the values:

for produce in prices:
    print prices[produce]

O meu codigo não funciona
#5

Ohhhhh thank you so much.