# 7/9 The Variance: Code returns invalid with error

#1

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

//// = indention because i don't know how else to show where indented

This code returns with the error "Oops, try again. grades_variance([0, 7, 6, 1, 4, 9, 2, 0]) returned 1.740537242 instead of the expected: 10.234375"
I'm not sure why this is occurring and don't know how to fix it. Can someone plz tell me how I can fix it

#2

There are a bunch of formatting tools in the text box where you enter your post. Please use those to ensure that the code looks just the way it should.

How many times are you supposed to do division in your function?

#3

variance += (average - score)**2
variance = variance / len(scores)

I think the problem is here, when you add a (average-score)**2, you divide it.
i returned variance/float(len(scores)) after you get the final variance += (average - score)**2

#4

É assim:

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

#5

Ok

• I assume you pasted your code on the editor, thus you want it to appear clearer (more code like) on the preview console.
So after pasting your code highlight it and press the `</>` button.

• For a more elaborate feel to your code , engulf your code in three grave accents followed by `python` (write code) then close it with three more grave accents.

• On your keyboard there is a `tab` button, that represents an indentation

so in essence my code looks like this

``````def grades_variance(scores):
variance = 0
for score in scores:
square_diff = (average - score) ** 2
variance += square_diff
total_variance = variance / len(scores)

``````

Hope it is readable for you.
Enjoy

#6

My Code:

variance = 0
for score in scores:
score = (average - score) ** 2
variance += score
last = variance / float(len(scores))
return last

#7

variance = 0
for i in x:
variance += (average - i) ** 2
result = variance / len(x)
return result

#8

you are dividing inside the for cycle, do it outside (result 334.071005917 )

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

#9
``````def grades_variance(scores):