Practice makes perfect: 3 - is_int


#1



guys help, i have no idea what is wrong

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

is_int(3.4)

is_int(4.0)

is_int(2)

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

[https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096]


#2

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.


#3

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
else:
return False


#4

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