# 6/9 Variance help

this is my code so far:

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

total = 0

return average

variance = 0
variance += (average - x) **2
return result

When I run it i get this error:

Oops, try again. grades_variance([5, 7, 5]) returned 0.1481481481 instead of the expected: 0.8888888889
29.4812243969
None

Not sure what I did wrong, any pointers would be greatly appreciated. Thank you

Update:
After naming the object like the description and playing around a bit with the order of things, I found this that works. So if anyone else has an issue with this problem this should work.

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

I typed in the same code as you, but I keep getting the error saying that the function returns None instead of the variance. Please help.

This worked for me. However, I do not completely understand how it works. If anyone could explain that, it would be great.

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
return variance / len(scores)

@boeingsbttrthnairbus which part of code you didn’t understand??

@c_a_dupin Your solution worked for me. The only difference I could see was that the

``variance += ((average - score) ** 2)``

has an extra parenthesis as opposed to:

``variance += (average - score) ** 2``

although this isn’t the first time I have written something twice without a real difference and the second worked.

I don’t understand this part:

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

It’s basically just order of operations.

``for score in scores:``

just means it will repeat once for each item in the list named scores.

``variance += x``

is another way of saying

``variance = variance + x``

and the

``(average - score) ** 2``

means that it will subtract the current score (from our score in scores loop) and subtract it from the average, then square that.
Sorry if it sounds like I am breaking this into little kid terms I just don’t know what part you don’t understand that needs to be explained. Hope this helped.

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

total = 0

return average

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

``````

This can help

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

total = 0
total += x

variance = 0
variance += (average - x) ** 2

To calculate the variance follow these steps: Work out the Mean (the simple average of the numbers) Then for each number: subtract the Mean and square the result (the squared difference). Then work out the average of those squared differences.

here check my code as well 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/ float(len(scores))
return variance

I could not get this exercise to work. Finally, I created a scores list (a grades list was already , but no scores list). That worked and let me move on to the next lesson. Here’s code below, hope it’s of help

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
scores = [95, 90, 80, 35, 7, 95, 80, 65, 85, 60, 70, 80, 45]

total = 0

return average

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

1 Like

I had the same problem. Simply changing the indent of “return” line to the same level of “for …” solved the problem. It happens often. How is it so?

Not to be “that guy” but why use a `for` loop for `grades_sum` when you could just use `sum(iterable)`:

``````def grades_sum(grades):