7 The Variance


#1
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_variance(scores)
    variance= 0 
    for score in scores:
        variance = float(grades_sum(grades)) - (grades_average(grades)) ** 2 
        variance / scores
        return variance
    
    print grades_variance(grades)

I get this error "Oops, try again. grades_variance([4, 2, 2, 0, 9, 7, 4, 8]) raised an error: maximum recursion depth exceeded"


#2

Hi
It appears in your code that the line print grades_variance(grades) lies inside your function grades_variance. You are thus calling the function inside itself and this is what is called a recursive function. I suppose that you don't want to use a recursive function, so I would recommend you to delete the indentation of the line print grades_variance(grades).

Cheerio


#3
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_variance(scores)
    variance = 0 
    for score in scores:
        variance = float(grades_sum(grades)) - (grades_average(grades)) ** 2 
        variance / scores
        return variance
    
print grades_variance(grades)

I am still getting something wrong is the way I try to understand what it is telling me to do?? idk, the is the error it is giving me maxium error depth exceeded
Traceback (most recent call last):
File "python", line 26, in < module >
File "python", line 19, in grades_variance
File "python", line 19, in grades_variance
File "python", line 19, in grades_variance
File "python", line 19, in grades_variance
File "python", line 19, in grades_variance <==== it shows this like 5000 times more..
RuntimeError: maximum recursion depth exceeded


#4

def grades_variance(scores):
    average = grades_variance(scores)

You are calling this function from within the function -- you can never escape!


#5

Okay, what does this mean "First, create a variable average and store the result of calling grades_average(scores)"?


#6

average = grades_average(scores)

#7

Now I get different errors"Traceback (most recent call last):
File "python", line 26, in < module>
File "python", line 23, in grades_variance
TypeError: unsupported operand type(s) for /: 'float' and 'list'"


#8

It's a bit hard to tell what is going on there because I don't know what you have on those line numbers.


#9

https://gyazo.com/3e7c57054a53b79861d1da6ad5bae653


#10

I'm just working though this exercise myself, but I don't think that your line 22 is doing what the instruction is asking you to do:

Compute its squared difference: (average - score) ** 2 and add that to variance.

I'm not sure why you are calling grades_sum and grades_average, you should just stick to what the instruction is asking you to do.


#11

Hi

I agree with @albionsrefuge, the way you compute the variance is way too complicated. You should stick to what is explained in the instructions :

04. for each score in scores: Compute its squared difference: 
(average - score) ** 2 and add that to variance

Moreover, the error message that you get :
File "python", line 23, in grades_variance
TypeError: unsupported operand type(s) for /: 'float' and 'list'"

is because the line 23 variance / scores is incorrect. You do not assign the result of the division to a variable. Finally, scores is an object. It is impossible to perform the division between a float (variance) and an object (scores). You should consider to divide by the length of scores instead.

Cheerio


#14