Home > other >  Return True if any number is even inside a list else return false
Return True if any number is even inside a list else return false

Time:10-12

I am new to Python. I am doing some exercise online.

def user_input_checking_even_number_list(user_input_list):
  
    for input_number in user_input_list:
        if input_number%2 == 0:
            return True
        else:
            pass
        
    return False

if I key run the code below

user_input_checking_even_number_list([1,3,5,8,9,11,20,21])

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?

CodePudding user response:

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.

  • Related