Variance


#1



https://www.codecademy.com/en/courses/python-intermediate-en-7mgOa/2/2?curriculum_id=4f89dab3d788890003000096


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)


#2

Point1.

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

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


#3

Hey greentreemee!
Thanks for quick response, yes the link is wrong and should be:
https://www.codecademy.com/en/courses/python-intermediate-en-7mgOa/2/1?curriculum_id=4f89dab3d788890003000096

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


#4

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?


#5

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


#6

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

Link

Edit**
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.


#7

K i'll take a look. thanks!


#8

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


#9

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:


#10

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?
thanks


#11

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


#12

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