6. the varience


#1




I'm sorry for asking a lot of questions, but i just got do it... i think my code is written right, but it keeps showing me this error: "Oops, try again. grades_variance([4, 0]) raised an error: global name 'score' is not defined"
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):
    sum_of_grades = grades_sum(grades)
    average = sum_of_grades / float(len(grades))
    return average

def grades_varience(scores):
    average = grades_average(scores)
    varience = 0
    for score in scores:
        varience += (average - score) ** 2
        a = varience / float(len(scores))
    return a

print grades_varience(grades)


#2

It is supposed to be grades_vari a nce, :slight_smile:


#3

Also, take out this line:

and just return variance / len(scores)
The reason why is because you are defining the variable a here, and you can't reach a variable that was initiated inside of the for loop, if you're outside of the for loop the way that your return statement is. :slight_smile:

I really hope this helped.

P.S. Your code is super amazing, it's so close to my own, even down to most variable names.


#4

OMG THIS IS SO EMBARRASING!!!!

thanks.... i feel so embarrased :cry:

OMG REALLY? that's so cool, we think alike!!!! :astonished:


#5

Your kind explanation helped me to understand the code. Thank you very much! :slight_smile:


#6

No problem! Glad to help! :slight_smile:


#7

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