Practice makes perfect: 3 - is_int


guys help, i have no idea what is wrong

def is_int(x):
    if int(x) == True:
        return True
        return False




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



well, int() will only validate integers (1,2,3,4,5 and so on) to true, for this exercise we also want to validate negative integers to true, and decimal numbers with a zero (for example 7.0) should also be validated as true by your is_int() function, int() won't validate 7.0 to true, given it is a float

it is even mentioned in the information of the exercise:

For the purpose of this lesson, we'll also say that a number with a decimal part that is all 0s is also an integer, such as 7.0.

This means that, for this lesson, you can't just test the input to see if it's of type int.


I searched for a formula to calculate for integers and it's:
if x/2 = 0, or (x+1)/2 = 0
The second part solves the odd integers.
To overcome the negative integer test, convert the number to an absolute value.

def is_int(x):
if abs(x)%2 == 0 or abs((x+1))%2 == 0:
return True
return False


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