FAQ: Learn Python: Student Becomes the Teacher - Part of the Whole

The class_list is the global object, students which contains references to three dictionaries. In a dictionary, a key-value pair is known as an item, but in this instance, item is referring to each referenced object, in turn. That would make it an element but we get the idea.

There is when we pass the students object as an argument of the call to get_class_average; as in,


You should have a students object in with your global data…

students = [lloyd, alice, tyler]

That line would, one thinks raise an exception.

for student in class_list:


There is nowhere mentioned that I should have a line “students = [lloyd, alice, tyler]”

1 Like

I think we do create it at some point earlier, but it may not be carrying over. Just insert that object underneath the three dictionaries so it is grouped with the data.

No, not even when fetching the code solution it is present. I think you might be refering to a later lesson.

That’s odd. It is needed for this function so I can’t see it being in a later lesson.

Ok I added it anyway.

Let’s take this one issue at a time.

For each student item in the class_list , calculate get_average(student) and then call results.append() with that result.

Okay, I understand the bold one. It instructs me to write “for student in class_list:”
But then I do not undersatand how to write the rest of the code. How to calculate?

You should have a function already written for get_average. We call it from within the loop, on each student in turn…

for student in class_list:

That will populate the results list with three different average grades.

I don’t quite understand what is happening here. Could you explain?

What does results.append does?

How can there be a “results” when that is not a variable that has been defined earlier? Calling implies a function…how can we call a function that has not been defined?

This is the code that the code solution provides:

for student in class_list:
student_avg = get_average(student)
return average(results)

1 Like

Just inside the function, we declare,

results = []

Then we proceed with the loop to iterate over the class list. Each calculated average is appended to the list.


def get_class_average(class_list):
results =
for student in class_list:
n = get_average(student)
return average(results)

print (“Lloyd: grade = %s, percent = %s”) % (get_letter_grade(get_average(lloyd)), get_average(lloyd))
print (“Alice: grade = %s, percent = %s”) % (get_letter_grade(get_average(alice)), get_average(alice))
print (“Tyler: grade = %s, percent = %s”) % (get_letter_grade(get_average(tyler)), get_average(tyler))
print (“Class average: %s”) % round(get_class_average([lloyd, alice, tyler]), 1)

Which returns this:

Lloyd: grade = B, percent = 80.55
Alice: grade = A, percent = 91.15
Tyler: grade = C, percent = 79.9
Class average: 83.9

Would you provide a link to the exercise? Why did you use a for loop in the function, yet four separate print() statements?

This is a bit late but you say to create the list students in the lesson after this one. It is the first step of the next lesson.

This did in fact work for me, but it did NOT return your below result. It actually returned only the letter “B”. I’m also curious as to why ‘n’ is used instead of student.

Could you please offer more insight?


how do I print the result?

These are the instructions:
Define a function called get_class_average that has one argument class_list . You can expect class_list to be a list containing your three students.
First, make an empty list called results .
For each student item in the class_list , calculate get_average(student) and then call results.append() with that result.
Finally, return the result of calling average() with results .

As you can see from my code, I already completed the first two steps:
loyd = {

“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]


Add your function below!

def average(numbers):

total = sum(numbers)

total = float(total)

return total / len(numbers)

def get_average(student):

homework = average(student[“homework”])

quizzes = average(student[“quizzes”])

tests = average(student[“tests”])

return 0.1 * homework + \

0.3 * quizzes + \

0.6 * tests

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"


return "F"

print get_letter_grade(get_average(tyler))

def get_class_average(class_list):

results =

Thanks so much !! Really appreciate the help !! :slight_smile:

With class_list never having been defined before in the code, how does the system know what class_list is outside this exercise?

Thank you for the help.

If class_list is written as a parameter in a function definition then it is locally declared and would have no role outside of the function, let alone outside of the exercise. Parameters are variables with local scope only.