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.