# The Variance

#1

I get this error message: `grades_variance([2, 3, 0, 9, 5, 5, 9]) returned 2.6348168083 instead of the expected: 9.9183673469`.

Where am I getting this wrong?

This is my code:

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

total = 0

return average

variance = 0
for score in scores:
variance += ((average - score) ** 2)
variance = (variance / len(scores)) #I think this is where the error comes from but I'm not sure what should be here.
return variance

#2

From looking at other forum posts it would seem that you need to return variance / len(scores) instead of what you have. Could be wrong though since I have not gotten to this lesson on Python. Where I looked was down below:

#3

You were right. One tiny detail could change the output of a whole code.

Whats the difference between:

``    return variance / len(scores)``

and

``````variance = variance / len(scores)
return variance``````

Shouldn't they both bring the same results?

#4

Note the position of the line marked with ###. The indentation is the problem, not the code.

``````    for score in scores:
variance += ((average - score) ** 2)
variance = (variance / len(scores))  ###
return variance``````

#5

Indentation?

i was really struggling with this, thinking the variance computation was wrong.

#6

Indentation is how Python identifies contiguous blocks for separating execution contexts and function scope. Loops `for` and `while` have blocks, as do `if..elif..else` statements in each case. Same is expected of functions.

``````def func():
# code block is indented``````

All code that has the same indentation is seen as having the same context. While indented, your line was executing with each iteration of the loop. Once the indent was removed, it only executed once the loop was completed. This is very important in Python.

#7

I get it now. Thanks

#8

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