I am new to Python. I am doing some exercise online.
JavaScript
x
10
10
1
def user_input_checking_even_number_list(user_input_list):
2
3
for input_number in user_input_list:
4
if input_number%2 == 0:
5
return True
6
else:
7
pass
8
9
return False
10
if I key run the code below
JavaScript
1
2
1
user_input_checking_even_number_list([1,3,5,8,9,11,20,21])
2
It will return True. But I have one question, is the for loop only check until 8 then the for loop will break? or it actually runs and checks until 21 even though the 8 is an even number that already meets the requirement?
Advertisement
Answer
At the time when your program was executing the statement return True
in your code, input_number
was 8
and for
loop had more iterations left. But return
statement causes this flow to break and immediately return to the code which called the function – which is outside of the function.