Hry guys, I'm wondering if anyone can explain why this code wouldn't work. I noticed that if I change line 24 to new_variance = variance / count and return new_variance on line 25 instead then I can pass this lesson. So I know how to pass the lesson, but I don't understand why.

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

def print_grades(grades):
    for grade in grades:
        print grade

def grades_sum(grades):
    total = 0
    for grade in grades: 
        total += grade
    return total
def grades_average(grades):
    return grades_sum(grades) / float(len(grades))

def grades_variance(scores):
    average = grades_average(scores)
    variance = 0
    count = 0
    for score in scores:
        variance +=(average - score)**2
        count += 1
        variance = variance / count
    return variance
print grades_variance(grades)



In your function grades_variance(scores) ,the final value of variance is changing at each iteration. adjust the indentation

If the link of exercise is right the you also have to define
grades_std_deviation() function (refer to instruction)


Hey greentreemee!
Thanks for quick response, yes the link is wrong and should be:

Where could I indent to keep the variance value the same?


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

If you take a look at it,you see its (variance = variance / count ) under for loop.
It means its inside for loop and its keep changing at every iteration,right?


Awesome! ok i figured it out. I'm starting to understand iteration a bit more lol. Thanks a bunch @greentreemee!!


Its very important.,Here it is a good link to read on indentation.


As I found that many people looking this thread so adding few more cool links may help more so here it is..

Quora - Indentation

A very good Answer from old CC forum.

Also this.


K i'll take a look. thanks!


my code was not working until i added the count variable and increment it as I found here...
I will be obliged if anyone can explain this to me...
thanks in advance
@greentreemee , @dempancakes


Hello @mahmud41,
The count is just another way to find number of items in list scores, We could have done it directly dividing the final variance with length of scores

Like this..

variance = variance/ len(scores)

I would suggest to read theory one more time,and try to figure out your own.
If you still feel stuck. I will help. :slight_smile:


I had the same problem
I found out I had to put the variance = variance / len(scores) outside the for loop and then it would work
this is only because the value of variance is changing each time right?


Yeah , as we put it inside the loop it will get re-assigned(at every iteration).


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