# Function "is_int" for determing numbers types "int" or "float"

def is_int (x):
if x-(-x) ==0.0 or x-(-x) ==0:
print True
elif x-(-x) > 0.0 or x-(-x) < 0.0:
print False

It gives back
“Oops, try again. Your function fails on is_int(-2). It returns None when it should return True.”

How to solve this???

You are `print`ing `True` or `False`. Your function should `return` True or False
Use `return` instead of `print`

Thanks for response
but gives “Oops, try again. Your function fails on is_int(-2). It returns False when it should return True.”

I think you should rethink your function, it returns `True` only for 0.

You aren’t testing for something where integers and non-integers behave differently.

Note that double negative is positive and,
0.0 will compare as equal to 0

You can check if x divided by 1 returns without remainder (use modulo operator `%`).
If there’s no remainder then x is an integer.

3 Likes

It worked, thanks very much for help

this worked for me
def is_int(x):
if x == 0 :
return True
if x is int :
return True

``````elif x - int(x) > 0 :
return False
elif x / int(x) == 1 :
return True
else :
return False``````

What I thought would be such a simple problem turned out to throw me a few curves. It was interesting to see different approaches to solving this. This is the code that worked for me. ``````def is_int(x):
x = abs(x)                #forces number to be absolute/positive
y = round(x)              #rounds x to nearest int
z = x - y                 #subtracts integer from float
if x - y == 0:            #if met, num is whole
return True
else:                     #else not whole num
return False

print is_int(-7.2)            #to call / test function
print is_int(5.0)
print is_int(-3)
print is_int(0)
print is_int(-8.3)
print is_int(12)
``````
4 Likes

Clear enough:

``````def is_int(x):
strong text if x-int(x) > 0 or x-int(x) < 0:
return False
else:
return True
``````
2 Likes

Nice one …

But z = x - y --> This line not required.

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

10 Likes

if z == 0 makes it clean or removing z = x - y.

Hah, well the modulo way is obviously the simplest way to go. I didn’t (and still don’t), completely understand modulo. So, following the instructions more literally, this was my solution:

``````def is_int(x):
if abs(x) - abs(int(x)) > 0:
return False
else:
return True
``````
1 Like

def is_int(x):
if x == 0:
return True
elif x % int(x) == 0 or x % int(x) == 0.0:
return True
else:
return False

I think this works as well.

this one worked for me:

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

4 Likes

I found this problem quite fun figuring out, this is what worked for me.

def is_int(x):
if round(x)-x==0:
return True
else:
return False

This is kinda simple, but it should work.

This is the simplest, by far.
Nice one.

2 Likes

I guess i did it the dirty way But this is how i solved it def is_int(x):
if x == int(x):
return True
elif str(x) == “0”:
return True
else:
return False