What is the problem with printing following code?


#1

I followed this code and want to print homework, but receive error. How can I print homework?

lloyd = {
"name": "Lloyd",
"homework": [],
"quizzes": [],
"tests": []
}
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]
}
lloyd["homework"]=[90.0,97.0,75.0,92.0]
lloyd["quizzes"]=[88.0,40.0,94.0]
lloyd["tests"]=[75.0,90.0]

students=[lloyd,alice,tyler]

for student in students:

print student
print students["homework"]

#2

Hi @simanii ,

This loop header establishes student as the variable that will represent each student's dictionary within the loop ...

for student in students:

Then, inside the loop, you can use the student variable, as follows, to access a value in the dictionary ...

print student["homework"]

#3

Thanks for your valuable answer,
Now, another related question. You suppose this code is as follows:

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]
}
def get_letter_grade(score):
if score>=90:
return "A"
elif score>=80:
return "B"
elif score>=70:
return "C"
elif score>=60:
return "D"
else:
return "F"
How is this code called? I mean I defined def get_letter_grade(score): ....
,but I do not call that in the main program. So, how is that calling?


#4

Hi, @simanii,

Codecademy calls your function in order to test it. If you would like to call it, you can do something like this ...

print get_letter_grade(96)

#5

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