3. is_int



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

might be a stupid question but why is -x not an integer according to the code?(google said it's an integer)

Replace this line with your code. 
def is_int(x):
    if (abs(x)==int):
        return True
        return False


Without using the built in type to test the value, can you think of another way?

if abs(x) == int

is not valid code, btw.


can u explain why it's not a valid code?


A number cannot equal a class, It is an instance of a class, int.

if isinstance(x, int) or type(x) == int:

We would not have to write it like that, I just did so we could see both methods of checking a value. We can check it for parent class, or we can check its data type.

But I still suggest you set this simple approach aside and think of a way to check without using either of the above comparisons. This is a good practice exercise intended for that purpose.


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