Exam statistics - the variance


#1

https://www.codecademy.com/courses/learn-python/lessons/exam-statistics/exercises/the-variance?action=lesson_resume&link_content_target=interstitial_lesson

File “python”, line 24
SyntaxError: ‘return’ outside function

Error should not be there

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

Not sure why the error is coming? Any suggestions?


#2

Be sure your return line (and the line above it) has an indentation.


#3
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/(len(scores))
return result

print grades_average(grades)

I tried this code but again getting the same error.

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

But if I try the below code,

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

print grades_average(grades)

ERROR: File “python”, line 23
result = variance/(len(scores))
^
IndentationError: unexpected indent

Please let me know what I can improve here? I tried every permutation.


#4

Mix of spaces and tabs. Do not use tabs to indent. Use four spaces as that is the industry norm.

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

#5

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