8. Why do we need to print key under the for loop?


Why do we need to print key under the for loop before we return the values? That's the part I left off and I'm not sure I understand why it is there?

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

8. Keeping Track of the Produce

on the second to last line:

why do you first type "price" (singular) then "prices" (plural) ???


@joey4242: That line of code was used to print the word "price" and at the position where %s is to replace it with the actual value stored on the dictionary who's key value is stored in the variable "key", got it?
So it's something like this:

  1. "price": string
  2. prices: name of the dictionary.

@autumnmurena: That's because the exercise might've asked you to print the key who's part of the key/value pair you're working with.
The first line will print the actual key value that key holds it, since that variable is iterating through the dictionary.
After that, you're accessing each dictionary, prices and stock, using the exact same key.


newDictionary = {
                 "Apple": 2.3, 
                 "Banana": 1.75
for key in newDictionary:
   print key #This will print the key value, left element.
   print newDictionary[key] #This will print the value at the position key, right element.

So if key's value is "Apple", the first line will print "Apple" and the second line will print the price of it which is 2.3

Hope I was clear!
Quote me if there's anything else I can help you with.

Best regards,