Function "is_int" for determing numbers types "int" or "float"


#1

def is_int (x):
if x-(-x) ==0.0 or x-(-x) ==0:
print True
elif x-(-x) > 0.0 or x-(-x) < 0.0:
print False

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

How to solve this????


#2

You are printing True or False. Your function should return True or False
Use return instead of print


#3

Thanks for response
but gives "Oops, try again. Your function fails on is_int(-2). It returns False when it should return True."


#4

I think you should rethink your function, it returns True only for 0.


#5

You aren't testing for something where integers and non-integers behave differently.

Note that double negative is positive and,
0.0 will compare as equal to 0


#6

any suggestions or help about how to think about the solution


#7

You can check if x divided by 1 returns without remainder (use modulo operator %).
If there's no remainder then x is an integer.


#8

It worked, thanks very much for help


#9

this worked for me
def is_int(x):
if x == 0 :
return True
if x is int :
return True

elif x - int(x) > 0 :
    return False 
elif x / int(x) == 1 :
    return True
else : 
    return False

#10

What I thought would be such a simple problem turned out to throw me a few curves. It was interesting to see different approaches to solving this. This is the code that worked for me. :slight_smile:

def is_int(x):      
    x = abs(x)                #forces number to be absolute/positive
    y = round(x)              #rounds x to nearest int
    z = x - y                 #subtracts integer from float
    if x - y == 0:            #if met, num is whole
        return True
    else:                     #else not whole num
        return False
    
print is_int(-7.2)            #to call / test function
print is_int(5.0)
print is_int(-3)
print is_int(0)
print is_int(-8.3)
print is_int(12)

#11

Clear enough:

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

#12

Nice one ...

But z = x - y --> This line not required.


#13

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


#14

if z == 0 makes it clean or removing z = x - y.


#15

Hah, well the modulo way is obviously the simplest way to go. I didn't (and still don't), completely understand modulo. So, following the instructions more literally, this was my solution:

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

#16

def is_int(x):
if x == 0:
return True
elif x % int(x) == 0 or x % int(x) == 0.0:
return True
else:
return False

I think this works as well.


#17

this one worked for me:

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


#19

I found this problem quite fun figuring out, this is what worked for me.

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

This is kinda simple, but it should work.


#20

This is the simplest, by far.
Nice one.


#21

I guess i did it the dirty way :confused: But this is how i solved it :slight_smile:

def is_int(x):
if x == int(x):
return True
elif str(x)[2] == "0":
return True
else:
return False