Standard deviation

While trying to get the deviation I keep getting a NameError: name 'variance' is not defined

Im out of ideas as to how this snippet could work as I expect it to return the deviation. Please assist. Here is the code:

def grades_std_deviation(variance):
    variance = 0
    for grade in grades:

        variance = grades_variance(grades)
    return variance** 0.5
print grades_std_deviation(variance)


I'm guessing here:
print grades_std_deviation(variance)
You are feeding in a variable variance. Do you have one created outside the function to feed i to it? Other than that i must have missed a typo.


I have variance defined in the previous function block below.

def grades_variance(scores):
average = grades_average(scores)
variance = 0
for score in scores:
variance += (average-score)**2

return variance/len(scores)

print grades_variance(grades)


The print statement cannot see that variable, hence the error. That variable is local to that function.

You need to assign grades_variance(grades) to a variable and pass that variable into grades_std_deviation().


Thanks, its really as simple as you say. I didn't know I could assign to the variable 'variance' after returning it.

def grades_std_deviation(variance):
    return variance ** 0.5
variance= grades_variance(grades)
print grades_std_deviation(variance)


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