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



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

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


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


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

Ohhhhh thank you so much.