Is_int


#1

What is the difference between the following two codes? Why am I getting an error message for the first one?

def is_int(x):
    a = abs(x)
    b = round(x)
    if a-b==0:
        return True
    else:
        return False
#Error: Your function fails on is_int(-2)

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

#2

in the second code, you take absolute (using abs() ) value of x, and store this in x

Then you round the absolutely value.

in the first code, you simply take the rounded value of x, not of the absolute value which you did in the first code.

to make the second work, you would need do:

b = round(a)

take the rounded value of absolute value

look:

print abs(-2) # will print 2

if you don't get the absolute value first, you round -2


#3

Yeah!! Got it..... Thanks a lot for the help buddy


#4