Is_int logic


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096


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
    else:
        return False


#2

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)
True
>>> is_i(3.1)
False
>>> is_i(-3.0)
True
>>> is_i(-3.1)
False
>>>

#3

Thanks for the input


#4

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