Error message: Oops, try again. Your function fails on is_int(-2). It returns False when it should return True.

I am stuck and I am honestly not sure which part of the code is wrong. Please help.

def is_int(x):
    y = x % 1
    y = str(y)
    for i in y:
        if i != 0:
            return False
            return True


could you explain what you think your code does? So i can correct where you wrong. I have seen many solutions to this problem, but this one is new



Basically I was trying to go through each character of the remainder to see if they were all 0; if so, x should be returned True as an integer, and vice versa.To do so, I needed to change the remainder from float to string, and took out any character that is not a number.

Here is the break down of the code:
Line 2: Get the remainder of x divided by 1; if X is an integer, it will be 0 or 0.00 (or even more 0 after the decimal)
Line 3: Change Y from float to string (this is for line 5 to successfully work)
Line 4: Take out the period if there is any, so in the case of integer, 0 will remain as 0 but 0.00 will become 0000
Line 5-7: If there is any character that is not 0 contained in the string (i.e., 0.04 will become 0004 and the code should return it to False), return False
Line 8-9: Or return True

Please let me know if there is anyway to make this code work. I have found the code that would work for this challenge but I still want to know if mine could work. Thank you!!


that is quit an interessting approach, now i see where you are going.

Yes, it is possible. First things first, here:

if i != 0:

i is of type string, so 0 should also be of type string:

if i != "0":

prove of concept:

if "0" != 0:
     print True

see how the condition evaluates to true? Which we don't want.

Then its important to understand that a function ends the moment a return keyword is reached, so in your case the function ends in the first run of the loop

but you only want to return True after we loop over all the decimal digits, so we want to place return True outside the loop


It worked and this is very helpful! Thank you so much!!