+ 1
Does return end a loop a little bit confused
def lol(x): for i in range(x): print(i) return lol(10)
2 ответов
+ 3
The function of return isn't to end a loop, but in this case its usage will do that anyways.
The purpose of return is to return a value from a function. Returning it allows for whatever value returned to be set to variables or printed to the console outside of the function itself, providing more functionality. How the return statement works is that once it is encountered within a function, it will immediately stop all execution of the function it is within continue to where it had been called from. In this case, because you're using a return statement within a loop, it will run the loop once, printing out the value of i for its first iteration, and then subsequently running the return statement which ends all execution of the function, stopping the loop entirely
+ 2
Yes it will end the loop.