Practice Makes Perfect: is_int


#1




I continually receive the message "Oops, try again. Your function fails on is_int(-2). It returns False when it should return True." -- I figured that my absolute value reference would solve for negative integers but that does not seem to be the case!


def is_int(x):
    if abs(x) == int:
        return True
    elif abs(x) == str(x) + "." + "0":
        return True
    else:
        return False


#2

Your function returns an error because of this line,

if abs(x) == int:

A number is an integer when it is divisible by 1 meaning to check if a number is an integer you may write,

if abs(x) % 1 == 0:

In doing so you do not require the elif block,

And are good on the else:,


#3

I started to play around with using '%' to identify the integer but kept getting it wrong. You're awesome, thanks for the explanation. This was quite helpful!


#4

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