Help On is_int Function


#1

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

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!


#2

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.


#3

That helped so much! Thank you. :slight_smile:


#4

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


#5

I would write:

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

correct?

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


#6

It looks correct, if it passes it is correct.

Good


#7

You need to add abs()


#8

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


#9

Would you elaborate, please?


#10

I see. Thank you again.


#11

from the documentation:
abs(x)
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)


#12

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


#13

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


#14

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()


#15

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.


#16

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