Review


#1


what's wrong with my code here?

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

It just keeps on saying "variance is not defined"
HELP, please


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):
    return variance ** 0.5
    variance = grades_variance(grades)
    print grades_std_deviation(variance)
    
print print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_std_deviation(variance)
print grades_variance(variance)


#2

In you grades_variance function, you are setting the variable to result and returning result. Set it to variance. The function should look like this:

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

#3

still not working,
It says "variance is not defined"


#4

That's because here:

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

You are only setting variance inside that function. You need to set it outside of the function like this:

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

You also don't really need that print grades_std_deviation(variance) because you print it out at the end already.


#5

tekkie1618 ....thank you!
It worked.


#6

@ujjwal1 No problem.


#7

Could you please explain me, what difference does it make , by keeping inside and outside ?
Why do we keep it outside ? why not inside?


#8

Because if you set the variance variable inside that function, the variable will not be set until the function is run, and in your case, the function is not run until the end of your code.


#9

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