Is_int


#1

Hi all,

Getting the below error message

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

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

`


#2

Use the modulo operator for this. Integers are divisible by one without remainder.


#3

You can use the function floor() from the library math... Look here

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

#4

yeah but want to get teh result the way it has been advised, do we have any possiblity of that?


#5

@konaesan that's not advised in teh instructions right?


#6

no.. But if you want to round down the number like they said, the function floor() is a way...
may be the real purpose of the exercise is to make you do some research...


#7

How do you mean? Advised by who? I guess konaesan's answer is closer to what you had in mind, but I didn't realise what you were trying to do in your original post.


#8

@konaesan it seems to have worked however am failing to understand the below

when I write the code as yours

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

it forks really good

however when I try to alter a bit and write as below

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

it fails what could be the reason?


#9

"Define a function is_int that takes a number x as an input.
Have it return True if the number is an integer (as defined above) and False otherwise."

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

Let's check:

1.0001 - 1 = 0.0001 > 0 #float
10.58 - 10 = 0.58 > 0 #float
6.99 - 6 = 0.99 > 0 #float
7.0 - 7 = 0 #integer

As you can see, x - floor(x) > 0 (the difference between a number and that same number rounded down is greater than zero...) only if is x (the number) is a float.
But the instruction says "return True if the number is an integer and False otherwise" so:

if x - floor(x) > 0:
    #it means x is float so you must return false
else: #in that case x - floor(x) = 0 because the difference between a number and that same number rounded down can never be < 0
    #means x is an integer so you must return true

#10

I’ve tried many things and still get this error. What am I doing wrong?

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

Ex 1
from math import *
def is_int(x):
if abs(x)-floor((abs(x))) == 0:
print True
else:
print False
Oops, try again. Your function fails on is_int(-2). It returns None when it should return True.

Ex: 2
from math import *
def is_int(x):
if x-floor(x) > 0:
print False
else:
print True
Oops, try again. Your function fails on is_int(-2). It returns None when it should return True.

Ex 3
from math import *
def is_int(x):
if x-trunc(x) ==0 :
print True
else:
print False
Oops, try again. Your function fails on is_int(-2). It returns None when it should return True.

Ex: 4
from math import *
def is_int(x):
if ((abs(x) - float(abs(int(x))))) > 0:
print True
else:
print False
Oops, try again. Your function fails on is_int(-2). It returns None when it should return True.


#11

You are using print where you are supposed to use return.

Print just puts out a value on the consoe, while return gives back a value to where the function was called.


#12

Thanks eveat

That was the problem. I was using print instead of return


#13

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

how about this?


#14

@swjeong this works out again how's it worked out and how is it different from the floor function?


#15

thank you making sense now.


#16

You're very welcome!


#17

i think the code below (same as mentioned by @swjeong) works perfectly:-
def is_int(x):
if x - int(x) == 0:
return True
else:
return False

To answer @deathscion
int(x) just takes the integer number and not the decimal for the number.
therefor if x is an integer, x - int(x) = 0,


#18

Isn't this the simplest way that's been demonstrated to this point?

def is_int(x):
if x % 1 == 0:
return True
else:
return False


#19

thank you @arrayninja61880


#21