1
def is_even(x) :
      while x:
           if x==0:
               return True
           elif x==1:
               return False
           x-=2
print(is_even(5)) 
print(is_even(6))

output False None

If the x==0 is replace with x==2 it works fine. Please explain why returning True is not working for x==0.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Daniel Deepak
  • 13
  • 1
  • 4
  • 2
    `while x` will only loop while `x` is truthy. If `x` is `0`, then it will break out of the loop. See: https://stackoverflow.com/q/39983695/6779307 – Patrick Haugh Oct 22 '18 at 16:50

6 Answers6

2

In the last iteration, x is reduced to 0 so the while loop is not entered, and the function is terminated. Since it doesn't explicitly return anything, it implicitly returns None, which is a false-y.

You could use a single if inside the while loop and use the while's condiiton itself to indicate an even number:

def is_even(x) :
      while x:
           if x==1:
               return False
           x-=2
      return True
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 1
    probably worth pointing out that if `x` will never hit exactly `1` or `0` (aka is not an integer, or is an integer `<0`, that this is just an infinite loop. – Adam Smith Oct 22 '18 at 16:54
0

If x = 0, then you fail your whilecheck and break out of the loop before you can check if x==0: return True.

You should instead use the modulo function, which returns the remainder of division.

3 % 2 == 1  # Odds
5 % 2 == 1
7 % 2 == 1

2 % 2 == 0  # Evens
4 % 2 == 0
6 % 2 == 0
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
0

Because when x == 0 it fails your while x check (0 is not truthy), so it exits the loop before it checks your condition again.

BTW the normal way to check parity is with the modulus (%) operator.

x % 2 == 0  # is_even
x % 2 != 0  # is_odd
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
0

If you use x as a condition while x that condition will be False if x equals 0.

The correct way should be:

def is_even(x):
      while True:
           if x==0:
               return True
           elif x==1:
               return False
           x-=2

Obviously this is a very weird way to check if a number is even, you can do:

def is_even(x):
      return not bool(x%2)
  • N.B. anytime your code is `if : return True; else: return False` you can instead write `return ` (or at least `return bool(predicate)`) – Adam Smith Oct 22 '18 at 16:54
  • Personally for clarity I would write `return x % 2 == 0` -- still short, no implicit conversion from int to bool – Two-Bit Alchemist Oct 22 '18 at 16:56
0

In Python, integers have True and False values. Any integer that is not 0, will always evaluate True, and 0 will evaluate False.

In your code you are using a while loop, which only runs if the subsequent statement evaluates True. When you check while x, if the value of x is 0 (due to the calculation inside the loop), your statement will be the same as while False, which will not run the code inside.

To avoid this issue you can use the modulo operation, which gives you the remainder of an operation. Hence, x % 2 will return 0, if x is even, and 1 if it is odd. You can make a check on that and return the correct value in fewer lines, using fewer operations.

return (x % 2 == 0)

The above statement will return True if there is no remainder, and False if there is.

nillyb
  • 36
  • 4
0

This should work

def is_even(x):
while x>=0:
    if x == 0:
        return True
    elif x == 1:
        return False
    x -= 2


print(is_even(5))
print(is_even(6))

in your code the loop terminates when x reaches 0, put a condition such that the loop runs even when x is 0

cheers