Is_int, why does this work?


#1



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


It's working just fine, but I'm not sure why the x == int(x) line works, or what it means.


I expected for "if x == int:" to work just fine, I'm guessing adding (x) at the end of int, somehow further defines what to consider an integer? or something along those line??? I'm really just spit-balling here. I could just accept that it works and it is what it is. But I want to understand WHY :slight_smile:

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


#2

int is a class; int() is a function.

>>> x = 3.14
>>> isinstance(x, int)
False
>>> x = int(x)
>>> isinstance(x, int)
True
>>>

>>> x = 3.14
>>> x == int(x)
False
>>> x = 3.00
>>> x == int(x)
True
>>>

Technically, though, we should not be using a built-in function in our practice code. Try to do it using some other means, such as that suggested in the hint.


#3

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