0
def check_even_list(num_list):
    # Go through each number
    for number in num_list:
        # Once we get a "hit" on an even number, we return True
        if number % 2 == 0:
            return True
        else:
            return False

now when execute function

[1]-check_even_list([1,2,3]) 
False 

[2]-check_even_list([2,1,3]) 
True 

why this True when #3 is return False ???

[3]-check_even_list([1,4,2]) 
False 

Why this False when #2 is return True ???

buran
  • 13,682
  • 10
  • 36
  • 61
Omar
  • 13
  • 3
  • 2
    The function only looks at the first value in the list. If it's even it returns `True` and if it's odd it returns `False`. The loop never reaches the second element of the list. – Tom Karzes Nov 14 '20 at 12:07
  • what is expected output of your function - e.g. for list with even numbers, list with odd numbers and list with mixed odd and even numbers? – buran Nov 14 '20 at 12:12
  • Check this out: http://pythontutor.com – mkrieger1 Nov 14 '20 at 12:22
  • I JUST want understand how this function work, @buran – Omar Nov 14 '20 at 12:26

3 Answers3

2

Your code will only ever check the first element, once a return statement is hit, no further code in your function will execute. Instead you could modify your code to

def check_even_list(num_list):
    # Go through each number
    for number in num_list:
        # Once we get a "hit" on an even number, we return True
        if number % 2 == 0:
            return True
    # After loop, must not have hit an even number
    return False

For conciseness, you could also write the equivalent

def check_even_list(num_list):
    return all(i % 2 == 0 for i in num_list)
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • did you mean the (return) on my code end the program immediately ? this is my first week on learning python and the conciseness is still need some time to me for understanding – Omar Nov 14 '20 at 12:49
0

You are using "early return statements". What your function does is actually only evaluating the first element of your list.

Here's I would write the function:

def check_even_list(num_list):
    return all(map(lambda x: x % 2 == 0, num_list))
  • Thanks, this is my first week on learning python, all,map and lambda function is still confusing for me – Omar Nov 14 '20 at 12:54
  • - `all` is a function that takes a list as a parameter. It returns either `true` if all items in the list are true, or `false` if one item of the list is false. - `map` is a function that takes 2 parameters: first another function, second a list. It will apply the function to every element of the list, one by one and return the list of results. - `lambda` is a way of declaring a local unnamed function. Here, the lambda takes one parameter `x` and returns the result of `x % 2 == 0` (eg: is x an even number) Note that the answer of Cory Kramer is the more pythonic way to write it imho. – Etienne Batise Nov 15 '20 at 01:23
  • Thanks, very good explanation – Omar Nov 19 '20 at 09:19
0

This is because once return statement is executed, function is not executed any more.

Use this code:

def check_even_list(num_list):
    # Go through each number
    for number in num_list:
        # Once we get a "hit" on an even number, we return True
        if number % 2 == 0:
            return True
    # After loop is completed and no even values encountered
    return False

and if you want to do just the opposite i.e. return false once an odd number is hit:

def check_odd_list(num_list):
    # Go through each number
    for number in num_list:
        # Once we get a "hit" on an odd number, we return True
        if number % 2 != 0:
            return False
    # After loop is completed and no odd values encountered
    return True
Satyajit
  • 115
  • 9