Function is_int is not working, please help


This is my code:

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

When I try it, it returns: Oops, try again. Your function fails on is_int(-2). It returns False when it should return True.

What's wrong? Please help.


This does not confirm that x is an integer.

if x // 1 == x

if x % 1 == 0

are two ways that should work. There are others.


Thanks, it worked. I was trying to find out how to divide on python.


This is regular division,

a / b

In Python 2, a / b will be an integer if both a and b are integers. If either is a float, a / b will be a float. Python 3 treats everything as a float unless we specify int().

This is floor division:

a // b

It will always yield an integer. JavaScript does not have this operator so we need to use the Math.floor() method.

The modulo operator is pretty much universal across JS, Python, Ruby, Java, etc.