9. Review - unsupported operand ** with 'list' and 'float' error?


#1

Hi everybody, I can't get past this as I get the following error:
Traceback (most recent call last):
File "python", line 37, in
File "python", line 28, in grades_std_deviation
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'float'

However, I don't see my code any different from that of everyone else.. I have a feeling that the std function might have a problem because it seems to always return the same value no matter the input (at least according to the codacademy console!!). My 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 / float(len(scores))

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_variance(grades)
print grades_std_deviation(grades)

Could you help me figure out what's wrong so that I can go forward to the next please?
: )


#2

I am having the same problem.


#3

Look, I've copied and pasted this instead from an archived post and it works!? Even though as far as I can see it look identical to mine! So weird!!

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):
    average = grades_sum(grades) / float(len(grades))
    return average

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

def grades_std_deviation(variance):
    return variance ** 0.5

variance = grades_variance(grades)

print "The class grades are:"
print print_grades(grades)
print "The sum of all grades in the class is", grades_sum(grades)
print "The class average is", grades_average(grades)
print "The grade variance is", grades_variance(grades)
print "Standard deviation is", grades_std_deviation(variance)

#4

I seriously doubt codecademy's interpretor has problems.


#5

Your problem is in your last line.

print grades_std_deviation(grades)

You gave grades_std_deviation grades as an argument, which is a list.

You want to give it variance as an argument (which is a float).

(You have actually done this correctly six lines above)


#7

Its kind of crazy but i took off the print statement and it worked, even though it asked for it.


#8

FWIW this passed. I'm just a noob so, I'm not sure why your code didn't work..

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
****variance /= float(len(scores))
****return variance

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

variance = grades_variance(grades) + variance

print 'grades:', print_grades(grades)
print 'sum:', grades_sum(grades)
print 'avg:', grades_average(grades)
print 'variance:' , grades_variance(grades)
print 'standard deviation:', grades_std_deviation(variance)


#9

This response helped me answer question 9 - http://discuss.codecademy.com/t/9-9-there-is-a-bug/5385/6?u=whyzar