Is_int


#1

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


Replace this line with your code.


#2

this:

        elif:
            x<0

should be one line i guess:

elif x<0:

that is fine, but what about -3.4? that is not a integer, and your code is currently not catching it


3. is_int
#3

the comment said fail on is_int(7.0)
def is_int(x):
if type(x) == int:
return True
else:
if x == str(x) + "." + "0":
return True
elif x=="-"+ str(x):
return True
else:
return False


#4

this condition:

elif x=="-"+ str(x):

won't check if the negative number is a integer.

at this point in the course, you have been taught abs() which gives you the absolute value. you could use this for both negative and decimal numbers to check if you are dealing with a integer


#6

try again. don't understand why it's not working.

def is_int(x):
    if "%.1f" % abs(x) == int(abs(x)):
        return True
    else:
        return False

#7

you are so close, here:

if "%.1f" % abs(x) == int(abs(x)):

why the .1f? just do:

if abs(x) == int(abs(x)):

this works because abs() gives the absolute value. lets do: -2.0 as example, the absolute value is 2. so that gives:

if 2 == int(2)

the integer of 2 is 2, so this is true.

Now lets do it for something which is not true, for example 3.4. the absolute value of 3.4 is 3.4, so:

3.4 == int(3.4)

the integer of 3.4 is 3, so this is false, as it should be


#8

Thanks a lot:slight_smile:


#9