6/9 Variance help


#1

this is my code so far:

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

print grades_variance(grades)

When I run it i get this error:

Oops, try again. grades_variance([5, 7, 5]) returned 0.1481481481 instead of the expected: 0.8888888889
29.4812243969
None

Not sure what I did wrong, any pointers would be greatly appreciated. Thank you

Update:
After naming the object like the description and playing around a bit with the order of things, I found this that works. So if anyone else has an issue with this problem this should work.

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


#2

I typed in the same code as you, but I keep getting the error saying that the function returns None instead of the variance. Please help.


#3

This worked for me. However, I do not completely understand how it works. If anyone could explain that, it would be great.

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)


#4

@boeingsbttrthnairbus which part of code you didn't understand??


#5

@c_a_dupin Your solution worked for me. The only difference I could see was that the

variance += ((average - score) ** 2)

has an extra parenthesis as opposed to:

variance += (average - score) ** 2

although this isn't the first time I have written something twice without a real difference and the second worked.


#6

I don't understand this part:

for score in scores:
variance += (average - score) ** 2


#7

It's basically just order of operations.

for score in scores:

just means it will repeat once for each item in the list named scores.

variance += x

is another way of saying

variance = variance + x

and the

(average - score) ** 2

means that it will subtract the current score (from our score in scores loop) and subtract it from the average, then square that.
Sorry if it sounds like I am breaking this into little kid terms I just don't know what part you don't understand that needs to be explained. Hope this helped.


#9
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
    result = []   
    for score in scores:
        variance += (average - score) **2
    return variance/len(scores)
    
print grades_variance(grades)

This can help


#10

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 x in grades:
total += x
return total

def grades_average(grades):
return grades_sum(grades) / float(len(grades))

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

print grades_variance(grades)


#11

To calculate the variance follow these steps: Work out the Mean (the simple average of the numbers) Then for each number: subtract the Mean and square the result (the squared difference). Then work out the average of those squared differences.


#12

here check my code as well :smile:

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

print grades_variance(grades)


#13

I could not get this exercise to work. Finally, I created a scores list (a grades list was already , but no scores list). That worked and let me move on to the next lesson. Here's code below, hope it's of help

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
scores = [95, 90, 80, 35, 7, 95, 80, 65, 85, 60, 70, 80, 45]

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))


#14

I had the same problem. Simply changing the indent of "return" line to the same level of "for ..." solved the problem. It happens often. How is it so?


#15

Not to be "that guy" but why use a for loop for grades_sum when you could just use sum(iterable):

def grades_sum(grades):
    return sum(grades)

Done. 4 lines of code reduced to 1.


#16

The code will stop when it hits return. So if you have a return insdie of a loop it will stop the first time the loops runs. If it is out side of the loop then your code will loop through the list and then you return what it has calculated.


#17

I see. That explains all those frustrating errors. Thank you.