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

one question:
I want to write a code in this exercise to get the averages of each student whit the “get_average()” function and put them in a list like this:
class_list = [lloyd, alice, tyler]
results = [80.55, 90.15, 79.9]

I wrote the below code, but I get this answer “None, None, None”

for student in class_list:
results = [ ]
result = get_average(student)
print results.append(result)

what’s the problem?

.append() does not return anything so the statement prints None.

1 Like

I am stuck. And now I am worried programming is not for me. Because with this lesson is when it get’s hard, so far our hand was held as it said in the text.

Can someone help me without providing the solution? In a way that I learn. For now I am stuck.

I don’t understand this:
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 .

There is no student item in class_list because the example only told me to to write the function with it as an argument.

This is my code.

def get_class_average(class_list):
  results = []
  for get_average(student) in class_list:
    results.append()
  return average(results)

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,

print(get_class_average(students))

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:
    results.append(get_average(student))

Thanks.

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:
    results.append(get_average(student))

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)
results.append(student_avg)
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.

I DID THIS:

def get_class_average(class_list):
results =
for student in class_list:
n = get_average(student)
results.append(n)
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?

Thanks!

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"

else:

return "F"

print get_letter_grade(get_average(tyler))

def get_class_average(class_list):

results =