Help On is_int Function


is_int Exercise:

I have been stuck on writing the is_int function for a few days and I would like an explanation of the part just before the instructions where it says:

"If the difference between a number and that same number rounded down is greater than zero, what does that say about that particular number?"

Would someone help me understand what they are trying to say? I feel like if I understand this bit, I'll be able to solve it easily. I don't want any code, just another way of thinking of how to solve the problem.
Thank you!


lets say i have 7.5, the floored number of 7.5 is 7. 7.5 - 7 = 0.5

So the number is greater then zero, so it is not a integer. You could use floor() to floor the number, you just need to import it:

from math import floor

Sill me, you can of course simply use int() to get the integer value.


That helped so much! Thank you. :slight_smile:


Did you solve it? I didn't spoil too much i hope?


I would write:

from math import floor
def is_int(x):
    if x - floor(x) > 0: 
        return False    
        return True


(And no, you did not spoil too much.)


It looks correct, if it passes it is correct.



You need to add abs()


You can use abs(), you don't need to. There are sometimes (very often in fact) multiply ways to solve a problem


Would you elaborate, please?


I see. Thank you again.


from the documentation:
Return the absolute value of a number. The argument may be a plain or long
integer or a floating point number. If the argument is a complex number, its
magnitude is returned.

so abs(-45) would give 45. very curious for @arrayrunner97311 explanations why you need this (need is such a strong word)


I was wondering myself... Thanks for the explanation.


-3.4 - (-3.0) < 0. Simple math.


if you use int() you need abs(), but if you use floor() you actually get 0.0 for (for example) -2, which is greater then 0. So it works

So you don't need abs()


I used the round function to round the number x down to 0 decimal places.

Then I followed the instructions to compare this rounded value of x to the original value of x.


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