Grade_average


#1



https://www.codecademy.com/courses/python-intermediate-en-7mgOa/1/2?curriculum_id=4f89dab3d788890003000096#


I have a bit of trouble in calling the other function. When I call grades_sum, I expect my second function to use the variable total but it is not able to and gives the following error message -

Traceback (most recent call last):
File "python", line 16, in
File "python", line 13, in grades_average
NameError: global name 'total' is not defined

I expect to use the variable total


[100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def grades_sum(scores):
    total = 0
    for i in scores:
        total += i
    print total
    return total

def grades_average(grades):
    grades_sum(grades)
    #average = total/float(len(grades))
    print total/float(len(grades))
    return total/float(len(grades))
    
grades_average(grades)


#2

total has a local scope, it only exist inside the grades_sum function

grades_sum returns the total, we can capture what the function returns by storing it in a variable, you know how to do this?


#3

is it by saying

from grade_sum import total

??


#4

that is importing modules/functions.

to capture what a function returns we need to call the function and store result of calling the function in a variable


#5

Awesome! That works!!

Thank you!


#6