Generally there's no reason to return x
unless the situation is more complex than this. If you only want to know if x is even, your code is fine except for two things: return x
at the end, and the actual even check. x / 2
will simply return x divided by 2; for x = 20
, this will return 10
. What you want is x % 10
; the %
operator returns the remainder of division. If x
is even, it returns 0
; else, it returns 1
.
However- if, for some reason, it is vital to your program that you return x
at the end of the function, I would recommend packing your values into a tuple:
x = 20
def MyEven(x):
result = False # Will remain False if x isn't even
if x % 2 == 0:
result = True
return (x, result)
This will return a tuple whose first value is the passed x
and the second is True
or False
depending on whether x
is even.