Part of the whole


#1


https://www.codecademy.com/courses/python-beginner-en-qzsCL/1/4?curriculum_id=4f89dab3d788890003000096

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:
        get_average(student)
        results.append(get_average(student))
        
        return average(results)


#2

Too much indentation. Should be outside of the loop.


#3

@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.


#4

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


#5

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