Your function fails on is_int(-2). It returns False when it should return True


I want the program to return True if x is an integer and false if it isn't. What could be wrong with this code?

It returns error: Your function fails on is_int(-2). It returns False when it should return True.

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


Your function should be reformatted to this:

def is_int(x):
    return x is int

Returning True when the result is True and False when the result is False is redundant, just return the result.

But since that is entirely equivalent in behaviour, that will of course not fix your bug. You are using the operator is, and you probably haven't been introduced to it and so what you wrote there is just.. English, not code. Being an operator, it has some specific behaviour, and that behaviour is not to test type. What the is-operator does is to test whether the value on the left and right side are the same value. Not just equal, but also the very same value. Two separate values would therefore not be the same, because they are equal, but not the same one.


So what alternative operator can I use instead of is


There isn't an operator for every program you might write. Read over the instructions a few times and ask yourself what properties your function should be testing, that says a whole lot about what the code should look like and it's indeed really difficult to write something before you know how something should behave.

The instructions explicitly say that you can't solve this by comparing the value to int, so you really must read the instructions more carefully before doing anything else


I have been struggling with this for quite a long time. Could you just show me how to go about this and explain it to me.


I got it to work.

This is it:

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


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