3

I assume that many of you are familiar with the CodeAcademy Python class. As the title says i am at the point where i have to get the average score of the class. Here it is what i did :

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

the error i get is "Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected". And i can't seem to find my mistake for 5 hours now, so please take a look and tell me what is wrong with the code.

Tim
  • 41,901
  • 18
  • 127
  • 145
Martin Spasov
  • 315
  • 3
  • 7
  • 23

1 Answers1

7

The indentation of your return statement is wrong. Currently, it is returning after the first iteration of the loop. Here is the proper indentation:

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

You can also simplify your code using list comprehension:

def get_class_average(students):
    return average(get_average(student) for student in students)
sshashank124
  • 31,495
  • 9
  • 67
  • 76