Code Error: Codecedemy expecting something else


#1

By running this code the program throws this error:
"You did not print the correct average. Double-check your code and make sure you print the average!"
But in the console:
7.69230769231
This is the code:

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

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

print grades_average(grades)

Thanks for your help :slight_smile:


#2

the mistake seems to be in your grades_sum

by default a function returns none at the end, if we want to return something else at the end of your function, we can use the return keyword

so, a function ends the moment a return keyword is reached (given returning is the last thing a function does)

so your function ends in the first iteration of the loop

but to get the correct total, the loop need to make all its iterations

how do you think we can make the loop do all its iterations?


#3

oh my mistake I should have done the return out of for loop. Now it works.
Thanks for your help :smile:


#4