It's Okay to be Average


i want to know about how this code works for the user?? plz explain in layman , i just need to clearify weather i thinking in right way or not.........[correct me if i am wrong- the process of adding all numbers and devide it by length are stored in variable(total) and further these variables are stored in function (average), so everytime user wants the average of numbers it will be process in function (average)?????..........................and we can name the function whatever i want to??

Replace this line with your code. 
def average(numbers):
    total= float(total)/len(numbers)
    return total


The variables are local members of a function object. A function is not a data structure to store anything. It does something on the data given to it.

We can name functions any way we choose (arbitrarily) but should keep in mind the reader and use descriptive names that give some idea of what purpose the function serves. In this example, average is a perfect name to describe what the function returns. An average when given a list of numbers.

item_sum / number_of_items => average

The name we give our local variables is given the same consideration: What information is referred by the variable? Take for instance the variable above, numbers. It is actually rather vague. We are averaging a list of grades so that would be a more descriptive term.

def average(grades):

Now the reader knows explicitly what the data structure contains.


thank you...clarifies a lot


def get_average(student):
homework= average (student ["homework"])
quizzes= average (student["quizzes"])
tests= average (student["tests"])
return .1*homework +.3*quizzes +.6*tests

can you help me in understanding this code
1. how the python will know, the numbers from which user is going to calculate the average is of homework or tests or quizzes.
2. in second line of code instead of writing the name of student(tyler,lloyd, alice) why we have written 'student' as we have not defined lloyd, tyler, alice as students anywhere in the code
3. plzz explain the processing of this code too


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