Is_int logic


Code is working. Just want to know if condition is sufficient to determine if number is integer.

Please specify any loopholes if any

def is_int(x):
    if x-round(x)==0:
        return True
        return False


It works for all values of x, greater or less than zero. No loopholes or edge cases. We can have it return a boolean without literally writing an if..else statement:

return x - round(x) == 0

>>> def is_i(x):
    return x - round(x) == 0

>>> is_i(3.0)
>>> is_i(3.1)
>>> is_i(-3.0)
>>> is_i(-3.1)


Thanks for the input


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