Variance Calculation Error


Continuing the discussion from Variance:

Thanks for your hint .Corrected the code.But now throwing a calculation error :- "Oops, try again. grades_variance([9, 8, 0, 5]) returned 0.0625 instead of the expected: 12.25"

The revised code is as below:

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

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

def grades_average(grades):
    total= grades_sum(grades)
    if total%leng==0:
    else: average=total/float(leng)
    print average
    return average 
def grades_variance(scores):
    average= grades_average(scores)
    for i in scores:
        variance=(average - i) ** 2
    return variance/len(scores)


Hi @arcace82508,

Here are some comments I have regarding your code:

1.. the variable grades is missing

Not sure if this is a copy-and-paste error, but check for it again :stuck_out_tongue:

2.. float...?

I am not very sure why you thought the if and else statements were necessary in this fact, they are a source of error in this code, because in this case when total % leng == 0 the average will become an integer, and not a float, which then causes your variance to become an integer as well.

What I mean is that when total % leng == 0, it does not necessarily imply that the variance is an integer, with no trailing decimal places. Sure, you can guarantee that the average will be an integer, but you can't say the same for the variance, particularly in the case of the score 50.5.

Hence, you should just do average=total/float(leng) for all cases :stuck_out_tongue:

3.. typo in calculation of variance

    for i in scores:
        variance=(average - i) ** 2

Looking at this, can you see where the error lies? Hint: You are missing a sign.

4.. printing grades_variance(grades)

You should print it in accordance with the instructions in order to pass the exercise haha

Hope this helps :slight_smile:


variance=(i - average) ** 2