Do I divide the total variance by the number of scores inside the loop?

Question

Do I divide the total variance by the number of scores inside the loop?

Answer

The step asking you to divide the total variance by the number of scores should be done outside of the for loop, otherwise your number will be far smaller than it should be because you divide it at each step.
Calculating a variance involves squaring the distance from the average of each score, summing those up, and then, after all that, dividing the summed variance by the number of scores.
In pseudo code, the process looks like this:

set variance to 0
for each score:
  variance = (distance of score from average) squared
return variance / number of scores

And be sure to print the result of calling your function at the end!

1 Like
grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]

def print_grades(grades_input):
  for grade in grades_input:
    print grade

def grades_sum(scores):
  total = 0
  for score in scores: 
    total += score
  return total
    
def grades_average(grades_input):
  sum_of_grades = grades_sum(grades_input)
  average = sum_of_grades / float(len(grades_input))
  return average

def grades_variance(scores):
  average = grades_average(scores)
  Variance = 0
  for score in scores:
    Variance = (score - average) ** 2
  return Variance / average  " or Variance / score gives me wrong results"
  
  
print grades_variance(grades)

What do I miss?

This should be accumulating. Use the assignment operator for addition, +=.

3 Likes

At the end, you also want the variance/ length of the ā€˜scores’ list. The length of the list of scores is not the same as the average of the scores

6 Likes

grades = [100, 100, 90, 40, 80, 100, 85, 70, 90, 65, 90, 85, 50.5]
def print_grades(grades_input):
for grade in grades_input:
print grade
print print_grades(grades)

In the ā€œprint_gradesā€ function, when you print out all grades one by one, how do you remove the ā€œNoneā€ that is being displayed at the end after all scores have been printed out?

By removing the print from the last line. Let the function do the printing as it is written.

If I remove the ā€œprint print_grades(grades)ā€ line, it loops forever and doesn’t end. I don’t understand what you are saying?

Do not remove the whole line, just the print keyword.

def print_grades(grades_input):
    for grade in grades_input:
        print grade
print_grades(grades)

Thanks, got it @mtf!

1 Like

That’s great. Did we get rid of the None?

Yes, got rid of it! Thanks @mtf!
I didn’t realize that in cases where we use print inside of a function where we don’t have to use return, we should not use the ā€˜print’ statement for calling the function. Got it now, thanks!

1 Like

how do i store something

how do i store something

That just means giving a value to a variable:

average = 5

stores 5 as the average.

When you’re asked to

create a variable average and store the result of calling grades_average(scores) .

You store the result of the grades_average function in the variable average:

average = grades_average(scores)

True to apply…
you should place the print after the for loop, instead of removing it.
here is an example:

inputlist = [3,6] outputlist = [9,11,12] for i in inputlist: if i > outputlist[-1]: outputlist.append(i) print(inputlist)