Is_int


#1


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

Hey all. I'm on this exercise. I don't think I understand what needs to be done. This is what I've understood and this is my code.

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

From looking through the forums, I've found out that this is the answer

def is_int(x):
    if x-int(x)>0 or x-int(x)<0:
        return True
    else:
        return False

I have no idea what is being written here. I don't understand why LESS or MORE than 0 is determining if the number is an int or a float. Could someone explain in a simple way for me?

Thank you
PM


#2

using int() won't be enough, because in the exercise -3 and 7.0 should also be considered integers to solve the problem of this exercise. Otherwise, it wouldn't be a challenge, would it? Then you can just use int()


#3

That makes sense, but how can >0 and <0 check the difference between 3 and 3.3? What is the process there?

I know this may be annoying, could you explain the code line by line for me? Because I understand parts, but not all of it.

<0 asks if any number is less than 0 (-5,-1,-23) >0 asks if any numbers is more than 0 (3,67,102) but how does it know the difference between a whole and a float number? Where in the code is that process?


#4

its here:

x-int(x)>0

if i have 7.0, int(7.0) would be 7. so then the math:

7.0 - 7 > 0:

which is false. while if i have 7.5 i cast to an integer gives 7 i get:

7.5 - 7 > 0:

is true. The < 0 is for negative numbers and is the same

This code doesn't even work, true and false should be swapped


#5

Ahhhhhh! I see. So you're taking the number away from itself as a whole number.

So when you round up 7, it becomes 7. SO 7 - 7 is 0.

But when you round up 7.5 to 7, 7.5 - 7 is NOT 0.

I understand now.

Sometimes it just takes a different way of explaining. Thank you for your help.


#6