Why is this definition after a for-loop needed?


#1

Hey guys, so I’ve tried the following code to pass the task:

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

Actually pretty clear, the functions are linked together to one… So I append the list “result” by the result of “get_average” of the item wich runs through the loop (called students in this list).
The Code always gave the error: The result is 85.5 not 91.5 as expected. So now the Solution from CC is the following:

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

So it actually does exactly the same but just declares the result as a variable and appends it by the variable.

Why is the result different?

Thank you so much for any answers,
Arthur :slight_smile:


#2

you should focus on this line:

       return average(results) # first code
  return average(results) # second code

see how the indent is different? What could this mean for our program?


#3

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