Get_class_average error


I'm stuck as to what's happening and why it's returning this error.

error message: (in the red box not the interpreter)

Oops, try again. get_class_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected

It should take the class list, read the averages and return the class average.

def get_class_average(students):
    for student in students:
        return average(results)


a function ends the moment a return keyword is reached

if a return keyword is reached in a loop, the function still needs to end, so the loop breaks

so your loop breaks in its first run/iteration due to return

so, your function will calculate an incorrect class average for any class with 2 or more students


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