Why is the average returned incorrect?


#1

Question

Why is the average returned incorrect?

Answer

Be sure to write your return statement outside of the loop, otherwise your loop can only possibly run one time before return is executed. As soon as a function executes a return statement, it exits immediately and gives whatever value that is.
Your return statement should be on the same indentation level as the for loop so that it’s inside of the function, but outside of the for loop.


FAQ: Learn Python: Student Becomes the Teacher - Part of the Whole
#2

2 posts were split to a new topic: Student is not defined


#3

My issue was declaring my var outside of my function:

wrong:

results[]
def get_class_average(class_list):

right:

def get_class_average(class_list):
  results = []