Variance Calculation Error

#1

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]

sum=0
for i in scores:
sum +=i
print sum
return sum

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

#2

Hi @arcace82508,

1.. the variable grades is missing

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

2.. float...?

I am not very sure why you thought the if and else statements were necessary in this case....in 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

3.. typo in calculation of variance

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

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