8/9 problem

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 += (average - score) ** 2
    result = variance / float(len(scores))
  return result

def grades_std_deviation(variance):  
  return variance ** 0,5

variance = grades_variance(grades)

print variance
print grades_std_deviation(variance)

So this is my code, i have now looked over it for 10 minutes and tried out multiple things…
“grades_std_deviation(6.6875) returned (1.0, 5) instead of the expected: 2.5860201082” is the error message that i get with the code from above. when i try to test grades_std_deviation with a number like 123 or something it always delivers a list (1 , 5).

The pproblem is probably pretty stupid and i will be ashamed, but i give up after comparing my code to others and checking over and over…


Check your indentation

The code should see a . not a comma.


calculate the variance of multiple grades consist of two parts:

calculating the variance of the grades in the list and adding them together. lets name this total_variance (the variable use for it is variance)

after successfully doing step 1, you need to divide the total_variance by the length (the number of grades).

And there is the problem, you do this each iteration of the loop, not at the very end after the loop

mtf explains why you get a tuple (immutable list)


I did, i think ^^ i do not get why grades_std_deviation would return a list :confused:


ohhh snap Thank you ill try to change that


Thanks for the reply! The badboy was the comma :smiley:


you are my hero thank you!


that was one of the two problems, what i mentioned was/is a problem as well


You’re welcome. The check indentation point is a boiler-plate suggestion.

The problem @stetim94 refers to is in this segment…

  for score in scores:
    variance += (average - score) ** 2
    result = variance / float(len(scores))    # <---
  return result

It’s actually hidden in that it does not affect the final outcome. The result will still be the sum of the squares divided by sample size. The assignment can/should be outside of the loop, and may even be returned without assignment.

return float(variance) / len(scores)


oops, auto pilot. Sometimes people do:

  for score in scores:
    variance += (average - score) ** 2
    variance = variance / float(len(scores))    # <---
  return variance

then it becomes slightly a problem. Sorry for the confusion @megawhiz22605


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