The Variance


I have no idea what i did wrong, and why it does not work as it shoud.
Please help me and point my mistake.

error message : Oops, try again. grades_variance([4, 0, 2, 3, 3, 0, 3]) returned 6129.9152578191 instead of the expected: 2.1224489796

and i get output of this code: 334.071005917

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

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
def grades_average(scores):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

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


Hi, @hubertsiwy ,

Should you be accessing the global variable, grades, within the grades_variance function here? ...

average = grades_average(grades)

What if we wanted the compute the variance for a different set of grades? Consider the role of the function parameter, scores.


True, my mistake there. but it did not help...
Your coment make me think and I change argument grade to score also in line 14 and 15. And it work fine now. Thank You.

Bot I don't understand why I have to change the code that was given from codeacademy? I thought I have to only write code below at 18 line as it was said in instruction


The code that was given when you began Exercise 7. The Variance probably carried over from the previous exercises.

The important issue here was to make sure that you made proper use of the function parameters.

Within each function that you create for the Exam Statistics exercises, you need to use the same name to access the list that you use for the name of the parameter in the function header. For the print_grades and grades_sum functions, you had grades as the parameter, and you correctly used the same name within the functions. However, for the grades_average and grades_variance functions, you had scores as the parameter name, but in some of the statements within those two functions, you referenced the name, grades, instead. In those functions, that name accessed the global list, grades, that was defined in the first line of code, because there was no local object named grades within the scope of those functions. This produced incorrect results when Codecademy tested your functions by passing them a list that was different from the global grades list.


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