3. is_int HELP!



<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
My code keeps throwing the following error: “Your function fails on is_int(-2). It returns False when it should return True.”

Here’s my code:
<What do you expect to happen instead?>


def is_int(x):
if type(x) is int and (x-x) > 0:
return True
return False

<do not remove the three backticks above>
I've been stuck on this lesson a LONG time and I've looked all over the web and have found nothing that's been super useful. please help =(


the first problem is this line:

(x-x) > 0

if you have a negative number, -2–2 = 0, so this condition is false. it should be (x - x)>= 0

Also, this programs wants 7.0 to return true, which type(x) is int doesn’t. You can use the modulo operator:

x % 1 == 0

if the number is a integer (or a float with a zero behind the decimal), this condition will be true



I think the easier part of your question is the (x - x) > 0. For all the integers, (x - x) == 0, which makes the logical result of the (x - x) > 0 will be False. And with an “and” logical judgement, the whole if statement will result in False only.

The trickier part is the type(x). The type() will return an integer data type. However, the is_int() want to check whether a number is a integer. Hence, you may not use the type(x) is int as a shortcut.
e.g. for 5.0, the type is float, but the number is an integer.

Hope this may answer your question.


A post was split to a new topic: Don’t understand why it’s wrong


the problem is in line 2, the if
this works
x == int(x)


Thank you! it worked =D