Is_int - why my code works


#1

Yo, I've done this example. But I have abit headache and I have problem to understand why my code works. Here is my code:

def is_int(x):
    if x<0:
        -x
    
    if x//1!=x:
        return False
    else:
        return True

3.is_int
#2

Hi, @acrodan ,

This part of your function is not technically an error, but it does not contrubite anything to the solution, so it should be removed ...

    if x<0:
        -x

This if header performs the essential task ...

if x//1!=x:

x//1 performs int division, which returns a value that is equivalent to the value x with any fractional parts removed. If the result of that operation is equivalent to the value of x, then the value of x is equivalent to an int, even if x is a float.


#3

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