Part of the whole


#1

why when I put
students=[lloyd,alice,tyler] out of the function , def get_class_average(students):, then it works? why?


#2

Hi @yokiaayuxi,

This needs to be outside the function ...

students=[lloyd,alice,tyler]

.. so that the function parameter does not get overwritten.

If you include it in the function, the function will process [lloyd,alice,tyler] even if a different list is passed to it. That defeats the purpose of the students parameter.


#3

The list is a global object and is not supposed to be created inside the function. Rather it is passed into the function to be enumerated. By writing it inside the function you wipe out the list that the user is passing in as their argument. The SCT sends in [alice] but the return is the result of [lloyd, alice, tyler] every time.

Lesson here? Keep your data outside of your functions and let the parameter do the work on anything it is given.


#4

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