I don't understand the difference between item and keys. (4. for the record)


#1

AT: 11.4 For the record

This is a long lasted question since I started the dictionary part.

So does python system understand what are 'item', or 'items', or 'key', or 'keys' without prior definition?

I tried:

for keys in students:
print keys["name"]
print keys["homework"]
print keys["quizzes"]
print keys["tests"]

for items in students:
print items["name"]
print items["homework"]
print items["quizzes"]
print items["tests"]

for keys in students:
print key["name"]
print key["homework"]
print key["quizzes"]
print key["tests"]

for item in students:
print item["name"]
print item["homework"]
print item["quizzes"]
print item["tests"]

They all work!

But the following does not work:

for keys in students:
print keys["name"]
print keys["homework"]
print keys["quizzes"]
print key["tests"]

or

for item in students:
print item["name"]
print item["homework"]
print item["quizzes"]
print items["tests"]

Where it shows

Oops, try again. Did you print out each student's test scores? It looks like Lloyd's test scores weren't printed correctly.

Where in the screen on the right hand side, the correct scores of lloyd are printed as expected, and there seemed to be no syntax problems.

Thank you! In other word, I am getting confused about:
the difference of keys and items.
Whether python accepts key or item as an internal component, without any user definition.


#2

look at the loop:

for keys in students:

keys gets defined in the loop, it is the iterator.

keys is now defined, and will get assigned each item from the list in order, which you can see if you would run this:

for keys in students:
    print keys

#3

Thank you so much! I tried

for abc in students:

and it also worked! How come the system understands it is the brackets of the students, say, 'quizzes', and 'tests', 'homeworks', but does not directly go to the numeric scores? How does the system figure out the two-layer dictionary and what we want?

Thank you!


#4

then abc is your iterator, like i said, this could be named anything you liked (i would still give it a logic name though)

well, a dictionary has key and values. quizzes and tests are the keys, which you can use the access the value


#5

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