Is_int()


#1



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


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


I feel like I'm missing something that was referenced in a previous exercise, but I cannot tell what it is. I know that I have seen this code used before in a previous exercise, if anyone could point me towards it, I would be grateful.


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


#2

Problem.

if x == int():

Can you explain what are you trying to do at this line of code?


#3

I was trying to get "if x is an integer, the following should return True." I think there is a better way of displaying this, however, and I know it was referenced before.


#4

If I had to check if x is an integer

I'd do something like this...

x = 4

type(x) == int


#5

Tried this code:

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

Error occurred:

Oops, try again. Your function fails on isint(7.0). It returns False when it should return True._


#6

Yep, You have to also check numbers like : 2.0, 45.00 , 67.00 (decimal part = 0),

because when you try that..

type(34.0) # it will return float not int

hint--

You can find decimal part of number and check if it is 0 or not!
If it is, then return True!
Can you think of any solution ?
if not then reply..


#10

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