Error: NameError: name 'name' is not defined


#1

Error: NameError: name 'name' is not defined

lloyd = {
"name": "Lloyd",
"homework": [90.0, 97.0, 75.0, 92.0],
"quizzes": [88.0, 40.0, 94.0],
"tests": [75.0, 90.0]
}
alice = {
"name": "Alice",
"homework": [100.0, 92.0, 98.0, 100.0],
"quizzes": [82.0, 83.0, 91.0],
"tests": [89.0, 97.0]
}
tyler = {
"name": "Tyler",
"homework": [0.0, 87.0, 75.0, 22.0],
"quizzes": [0.0, 75.0, 78.0],
"tests": [100.0, 100.0]
}
students=[lloyd,alice,tyler]
for i in students:
print name[i]
print homework[i]
print quizzes[i]
print tests[i]


#2

Think in terms of singular/plural.

for student in students:

Now we have a dictionary,

print student['homework']

and so on.

A step forward...

Rather than having four print statements, we could iterate over the dictionary keys and have only one print statement.

for key in student:
    print key, student[key]

#3

for student in students:
But "student" variable is not defined!


#4

Yes it is, in the very statement.

for this_is_the_iterator_variable in iterable

#5

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