Part of the Whole - What am I doing Wrong


Here is the code I use

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 average(numbers):
total = 0
total = sum(numbers)
total = float(total)/len(numbers)
return total

def get_average(student):
homework=average(student[“homework”])* 0.1
quizzes=average(student[“quizzes”])* 0.3
tests=average(student[“tests”])* 0.6
return homework+quizzes+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(lloyd))

def getClassAverage(students):
aver= (get_average(lloyd) + get_average(tyler) + get_average(alice))/len(students)
return aver

print getClassAverage(students)

This is the error I get “Oops, try again. get_class_average([alice]) returned 83.8666666667 instead of 91.15 as expected”


I am also intenting properly - just doesn’t show up here


I can’t see a clear reason why the code wouldn’t execute but it’s really not what the instructions asked you to do.

2.) First, make an empty list called results.
3.) For each student item in the class list, calculate get_average(student) and then call results.append() with that result.
4.) Finally, return the result of calling average() with results.

When they say “For each” that should tip you off that your supposed to write a for loop.

In your getClassAverage function, the way you wrote it, you assume there can only be 3 students, lloyd, tyler, and alice in a class. However if we added a 4th student named “steve” your function would not calculate the average correctly.

A better way to write this function would to loop over all the students in the class, so the function is independent of the number or names of the students; and could be used on ANY class of students

for student in students:

That way the function will loop over all the students regardless if there are 3 or 100. By appending each average to the results list we can simply average that list when we return the class average

Edit: When I started taking this lesson I had already skipped ahead to Unit 8 Lesson Loops, so this was clear to me. Looking at the class structure this section is actually before the Loops part so I can see how this would be confusing to people who haven’t gotten to the loops part yet. I’m not sure how they expect you to write this function properly without loops. The above worked for me, I’d suggest checking out Unit 8 for more info.