9/9 Review: Why doesn't this work?


#1

I get the following error on the code below:

Traceback (most recent call last):
** File "python", line 36, in **
NameError: name 'variance' is not defined

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

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

print "The grades are: ",print_grades(grades)
print "The sum of all grades = ",grades_sum(grades)
print "The average grade scored = ",grades_average(grades)
print "The variance is = ", grades_variance(grades)
print "The standard deviation = ",grades_std_deviation(variance)

#2

I read this topic and it makes me think that you have not read the error message that you posted. It tells you where you are accessing a variable that has not been defined. Is it your opinion that it should have been defined? If so you'll have to explain that.


#3

If I change line 36 to:

print "The standard deviation = ",grades_std_deviation(grades)

I get the following error:

Traceback (most recent call last):
File "python", line 36, in
File "python", line 28, in grades_std_deviation
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'float'


#4

Hi @pedropandro ,

Originally, this was the final line in your code ...

print "The standard deviation = ",grades_std_deviation(variance)

Evidently, you intended to pass a value representing the variance of the grades to the grades_std_deviation function. But, as the error message and @ionatan have pointed out, you cannot do that successfully by passing a variable that has not been defined. Can you think of a way to assign an appropriate value to variance prior to using it as an argument in the function call? Keep in mind that variables defined locally inside functions are not the same as global variables.


#6

hey pedropandro here is the right 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):
average = grades_average(scores)
variance = 0
for score in scores:
variance += (average-score) ** 2
return variance/len(scores)
print grades_variance(grades)

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

print grades
print print_grades(grades)
print grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
variance = grades_variance(grades)
print grades_std_deviation(variance)


#8