8. Part of the whole


#1
def get_class_average(class_list):
  results = []
  for student in class_list:
    student_ave = get_average(student)
    results.append(student_ave)
    return average(results)

The above is my code. It keeps telling me that it returns 91.15 instead of 85.85, but the suggested code is as follows:

def get_class_average(class_list):
  results = []
  for student in class_list:
    student_avg = get_average(student)
    results.append(student_ave)
    return average(results)

Could someone please tell me what is different??


#2

the suggested code isn’t good either.

By default, a function returns None at the end of the function

if we want to return something else at the end of the function, we can use the return keyword, thus the function ends when return keyword is reached

so your function ends in the first iterations of the for loop, but to get the correct class_average, the loop needs to make all its iterations


#3

Thank you for your reply


#4

A post was split to a new topic: Make sure that you have defined get_class_average


#6

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