Getting a headache with what should be a simple function:
def is_int(x):
isinstance x, int)

This built in function in my function is_int returns false on -2, why, I'm not sure.
Anyone able to shine some light on this? Thanks!


Could you please tell me, what exactly your code is doing? Or could you just tell me the code of isinstance?



It should return True if x is an integer, false otherwise.
And this particular case, 7.0 should be an int, as well as 7.00, 7.000, etc.
7.1, 7.02, etc. would be False.


If you did learn type() already, you could check the input like this:

def is_int(x):
    if "int" in str(type(x)): #You turn the type into a string to check it and then check if it has int in it
        return True
        return False

or if you haven't learned it yet, you could go with:

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

What you are doing here, is to check the x, if it is the same if you turn it into an integer.

I hope this helps you :smiley:


Thank you so much frozentofu,

That second option was what they wanted.

All the best to you!


You're welcome, glad if I could help :smiley: