Shallow understanding


#1



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


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:
results.append(get_average(student))
return average(results)


#2

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.


#3

That was a very useful answer, thank you!


#4