Practice makes perfect 2/15


#1

Hi guys, I have solved the exercise but I was just wondering why the below function returns me an error.

def is_even(x):
if x/2 == int:
return True
else:
return False

Thanks
Ilias


#2

There are two problems in your code.

First is strictly logical. You can't check if number x is even by checking if x / 2 is an integer. Why? Because 7 / 2 = 3 :smile: You have to divide this number by a float value -> 2.0.

Second problem is that you can't check if number is integer by checking if it's equal to int. You can use int function:

if x / 2 == int(x / 2.0):

or isinstance function:

if isinstance(x / 2.0, int)

#3

easiest way is if x%2 == 0


#4

thank you.

I forgot that dividing and integer by an integer gives an integer! Thanks again :smile: