Is_int


#1

I am struck in the level 3 of practice makes perfect:
No idea what to do? Can someone explain this in detail?

def is_int(x):
if x == type(int):
return True
else:
return False

How it will check if it's floating integer nearer to zero?


#2

You can use
if x % 1 == 0
to check if the number is an integer. Naturally integers give a number with no remainder when they are divided by 1, so they will have a remainder of 0 when divided by 1 with the modulo function.

You can't use if x == type(int) because numbers with decimals are not the type int, although they can be integers e.g. 75.0 would return False.


#4

def is_int(x):
if x % 1 == 0 and x % 1.0 == 0:
return True
else:
return False


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.