Stuck on review


#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_average(scores)
v = 0
for i in scores:
v = v + (average - i)**2
return v/len(scores)

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

grades_sum(grades)
print grades_average(grades)
print grades_variance(grades)
print grades_std_deviation(grades)

I get the error "Oops, try again. grades_std_deviation(1524.6666666666667) returned 18.2776094147 instead of the correct value 39.0469802503" for the above code.

I suspect that it's due to the fact that for the function grades_std_deviation(variance), variance is always equal to grades_variance(grades) ** 0.5 regardless what the input is.
However, I am not able to find a solution to solve this problem.
Can anyone offer some help please?


#2

Seamantically, this makes more sense to me:

average = float(sum_of_grades) / len(grades)

since the sum is likely to be a float. len() is a counting number so one would never think of it as a float. Not the problem, though. Just pointing this out.

Technically, since this is a function chain, the most logical place to declare the float is in the first function in the chain.

total = 0.0

or

total = float(0)

Now all values being passed around will be floats.

This is the problem function. You are not returning the square root of the value passed in to the parameter.


#3

Thanks your for help mtf, but can you elaborate more on the problems of the last function? I still dont quite understand it.


#4

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

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

I have found a correct version of the code.
However, I could not understand what happens here:
"return variance ** 0.5
variance = grades_variance(grades)
print variance"

why would you return the value before you define variance as grades_variance(grades).
Furthermore, why does it only work when you print grades_std_deviation(grades_variance(grades)?

I hope someone can help me clarify this for me. THanks.


#5

grades_variance returns sigma squared.

std_deviation should return just the square root, or sigma.

def grades_std_deviation(variance):
    return variance ** 0.5

#6

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