Get_class_average error


#1



https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

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):
    results=[]
    for student in students:
        results.append(get_average(student))
        return average(results)


#2

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


#3

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