Having trouble with review 9/9


#1

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
#grades = [1, 4, 2, 3, 5, 7, 3, 8, 4]

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

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

the error i get is this :

Oops, try again. grades_variance([47, 6, 49, 50, 92, 68, 35]) returned 10920.4837278106 instead of the correct value 608.2448979592

EDIT: everytime i execute this exercise i get a different answer. for example :

grades_variance([74, 53, 21, 51, 7, 37, 51, 16, 37]) returned 19368.2263313609 instead of the correct value 399.1358024691

BUT, it prints this

1045.5
80.4230769231
4342.92307692
65.9008579377
None

Thanks for the help in advance. :slight_smile:


#2

Bugs exist in this part of your code...

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

Point1.

average = grades_average(grades) but your input/parameter is scores ?

Point2.
Back to previous exercise when we had to build function to calculate variance..

It was written..

Divide the total variance by the number of scores
You need to add one line of code there..

point3.
also this exercise tells you to do these..

Print out the following:

all of the grades
sum of grades
average grade
variance
standard deviation

But you haven't used your print_grades() function ?


#3

dude you are a legend hahah. :smiley:


#4

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