Need help with is_int

#1

I am stuck at the is_int part of Practice Makes Perfect

The error message that is displayed reads, "Your function fails on is_int(-2). It returns False when it should return True."

``````import math
def is_int(x):
if abs(x) - abs(round(x)) > 0:
return True
else:
return False``````

#2

reverse the order of nesting...

``round(abs(x))``

#3

Still returns the same error message as before.

#4

Yeah, it won't matter which order the nesting takes place.

It's actually a problem with `round()`

``abs(-3.78) - abs(round(-3.78))``

is not greater than zero.

#5

Why I had that in my code was because of this statement in the instructions, "If the difference between a number and that same number rounded down is greater than zero, what does that say about that particular number?"

So, since that does not seem to work. I am at a lose for what to do.

#6

Never mind I found the solution.

import math
def is_int(x):
if abs(x) - abs(round(x)) != 0:
return False
else:
return True

#7

Let's consider a helper tool...

``diff = lambda x: abs(x - round(x))``

Now we have a function that returns a positive difference or zero.

``````diff = lambda x: abs(x - round(x))
def is_int(x):
return False if diff(x) else True

print (is_int(-3), is_int(3.14), is_int(-3.14), is_int(99))

# True False False True``````

If you are not yet familiar with `lambda` just think of it as an anonymous function expression stored in a variable. We call it the same way we would a function.

Refactoring

``    return False if diff(x) else True``

may be reduced to a simple expression, rather than a ternary.

``    return not bool(diff(x))``