7. The variance - getting a strange error


Ok, so I'm trying this, and I keep getting an error, I think when codeacademy tries to substitute other values as a test.

This is where I'm failing.

Oops, try again. grades_variance([7, 0, 1, 8, 5, 2, 6, 3, 7]) returned 5797.2046351085 instead of the expected: 7.5555555556

When I run it for grades I get the right values, is there some way to bypass this extra test, or is my code just wrong somewhere? I look at a number of other responses and the codes seems right.

I think the site is broken? If I add the set of numbers from the error into the grades list the variance itself works fine but I get a different error then:

Oops, try again. grades should contain [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]!

Any ideas of what I might be doing wrong would be appreciated.:cry:

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
#grades = [9, 1, 7, 5, 6, 2, 9, 3]
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(grades):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

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

print grades_variance(grades)


Average must be scores from grades_average instead of grades, hope this helps!! :slight_smile:



Thank you, I had to change all the variables to scores and it worked!



I have a strange error, too: For me it says that I wouldn't have defined the parameter "average" although I did :unamused:

I cannot proceed that way :confused:


I think you named average on the wrong line here, it should be on line 19

    average  =  grades_average(grades)

As it stands you are calling on a variable from another function and it won't work.


So I really can use "average" as another parameter ? I thought it would be universal once defined, but it it seems I was wrong...


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