8/9 Standard Deviation


#1

My code seems to function, but it's not giving out the correct standard deviation. Here's the code:

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_variance(scores):

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

print grades_variance(grades)

def grades_std_deviation(variance):
> variance = grades_variance(grades)
return variance ** 0.5

print grades_std_deviation(grades)

This is the error I get:

Oops, try again. grades_std_deviation(8.75) returned 18.2776094147 instead of the expected: 2.9580398915


#2

I think, the error may be due to the same variable name "variance" used in function argument and again in storing results after calling function grades_variance(grades). You may put the line variance = grades_variance(grades) outside the function. The below code works correctly.

def grades_std_deviation(variance):
return float(variance) ** 0.5

variance = grades_variance(grades)
print grades_std_deviation(variance)


#3

That last one didn't work. Here's my take 2 along with the error message:

def grades_std_deviation(grades):

variance = grades_variance(grades)
return variance ** 0.5

Oops, try again. grades_std_deviation(7.5) raised an error: 'float' object is not iterable


#4

This worked for me:
def grades_std_deviation(variance):
return float(variance) ** 0.5

variance = grades_variance(grades)
print grades_std_deviation(variance)

#5

This is my code :smile:

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_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        variance += (average - score) ** 2
    result = variance / len(scores)
    return result

print(grades_variance(grades))

def grades_std_deviation(variance):
    result = variance ** 0.5
    return result

variance = grades_variance(grades)
print(grades_std_deviation(variance))

#6

Your code still works without the line "variance = grades_variance(grades)". FYI.


#7

yh this worked for me


#8
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_variance(scores):
    average = grades_average(scores)
    variance = 0
    for score in scores:
        square_diff = (average - score) ** 2
        variance += square_diff
    total_variance = variance / len(scores)
    return total_variance
    
def grades_std_deviation(variance):
    sqr_root = variance ** 0.5
    return sqr_root
    
variance = grades_variance(grades)
print grades_std_deviation(variance)

#9

Thank you! That solved all my puzzle!:grinning:


#10

Can anyone explain please why we are creating a new variable after the function?? or why does not new variable work in the function?Thanks!

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_variance(grades):
    average = grades_average(grades)
    variance = 0
    for grade in grades:
        variance += ((average - grade) **2)
    return variance / len(grades)
       
        
print grades_variance(grades)

def grades_std_deviation(variance):
    variable = grades_variance(grades)    #why would not it work here 
    result = variance ** 0.5
    return result 
variance = grades_variance(grades)
print grades_std_deviation(variance)

#11

You are great!
Your code works!!
Thank you!!


#12

i think this is how variables work..
any global variable can't use a local variable,
however a local variable can use the global variable,
and a local variable cant use another local variable( possible only in 2 or more functions)..
for eg:( variables with same name can be used if they are in a local variable under a different function.


#13

Help. May I ask what result you got for standard deviation? I am stumped. I think that ideally we should get 18.28. For some reason it allows 334.01
def grades_std_deviation(variance):
standard_deviation = variance**0.5
#return variance**0.5
return standard_deviation
variance=grade_variance(grades)

print grades_std_deviation(standard_devition)

Note that the last print statement is indented. When unindented, I have the message "variance is not defined"


#14

Oops excuse typo should be standard_deviation. My apologies


#15

Thank you folks. I now know what I was doing wong. I have got it now. Thank you all