Part of the whole


Oops, try again. getclass_average([alice, lloyd]) returned 91.15 instead of 85.85 as expected_

This is the error when the return function is part of the for loop as shown in the code below. someone explain why the result behaves differently when the return average(results) is under the for loop function.

the code works well when the return average(results) is put in the same indentation block as the for loop.

explain why the change in the indentation produces a different result. Thanks

def get_class_average (students):
    results =[]
    for student in students:
        return average(results)


Too much indentation. Should be outside of the loop.


@textmaster56962 knows that the indentation is wrong. He wanted an explanation as to why the indentation needs to be be the way it needs to be.


a function by default returns None at the end, if we want to return something different at the end we can use the return keyword. This does mean the function ends

so if you place return inside the loop, the loop breaks in the first iteration, the function ends.

It should be known now, that python uses indention to determine what is nested inside loops, function and so on


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