Shallow understanding


Hello! I actually already figured out that I need to put the "results" list on the inside of the function. I was just curious as to why I can't initialize it on the outside of the function. Forgive me if this should be obvious; my brain isn't making sense of this for me.

Error message: Oops, try again. get_class_average([lloyd]) returned 85.85 instead of 80.55 as expected

students = [lloyd, alice, tyler]
results = []

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


Hi, @datamaster36428,

To calculate a correct class average, results needs to begin as an empty list prior to each time you call the get_class_average function. If this statement is outside the function ...

results = []

... then results gets initialized only prior to the first time you call the function. Thereafter, it will contain data from previous calls to the function.

Codecademy tests your function based on the expectation that each time it is called, it bases its calculation only on the data that it is passed during that particular call.


That was a very useful answer, thank you!