3/15 Practice Makes Perfect - Help


I've tried many ways - This is my current code

def is_int(x):
if type(x) is int:
return True
elif x - round(x) > 0:
return False
elif x + round(x) < 0:
return False
elif x - round(x) == 0 or x + round(x) == 0:
return True
return False

I Was hoping someone could put me in the right path?
Thankyou In advance


Hey Doffylaw,

Since strings and integers look different to the computer ("1" is not the same as 1), if you turn x into an integer, then compare it to the original x to see if they're the same, you can then return either True or False depending on whether they're the same or not :)

I hope this helps, please let me know if you need any more help or have any questions!


Can you please elaborate on this idea?


It's simple.
First check for the type int and return True.
Next, in elif, if x-round(x) == 0 return True
In else, return False

Small code. No need to make it lengthy and messy :smile:


Yes you can make it sure easy or not, if you just want to return whole numbers you can doing something like the following.

Pass a list

def is_whole(entry_list):
    return [num for num in entry_list if type(num) == int]

Pass a single Entry

def is_whole(number):
    if number % 1 == 0:
        return number


def is_whole(number):
    if type(number) == int:
        return number


Sure, @ranasi_chandl20.

I think the simplest way to check if your number is an int or not is to run a simple check, like:

if int(x) == x:
  # do stuff here!

The reason that works is because if you get a string as an input, it'll look like this to the computer:

# or, with a number:

whereas an int looks like


with no quotes surrounding it. if "1" == 1 doesn't trigger, because they're different, but if 1 == 1 will trigger, because they're the same. Converting something like "1" to an int (it's currently a string), then checking it against the original lets us know whether our input was a number or not.

I hope this helps, feel free to ask any more questions you may have!



If you check the type like that you will always throw an error if it is not the right type.


x = 'string'
if int(x) == int: print(x)
#Throws an error

if type(x) == int: print(x)
#This will not throw an error because we are asking the type and then comparing
#the return value of type function to int.


You don't have to call other functions:

def is_int(x):
    if 10 * x % 10 != 0:
        return True
        return False


Thankyou, this helped me understand it better


import math

n=input("Put your number: ")


def is_int(x):
if (x - math.floor(x) == 0):
return True
print "This is a integer number!"
return False
print "This is not a integer number!"

print is_int(n)

This is the code that I use and its work.
Good luck!


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


Heres the simplist way

def is_int(x):
if x == int(x * 1.0):
return True
return False


it converts x to a float then checks if it is an int all in one line


Another one-line approach that works is to convert the input to an integer and compare the absolute value of the converted input to the absolute value of the raw input:

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


if x**2%1 == 0:
return True


Casting seems pretty advanced for people just learning to code, but you can also use that to check for values that are considered integers just in this lesson (such as 7.0).

myInt = int(7.0)
print myInt

Will yield


You can subtract a double casted to an int (which truncates the decimal) from the original double. If the result != 0 then you know the original double is not an int.


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

Everyone else appears to add more than necessary.


@glavezsvcoder That's what I've got, see my post above :)


i think this is smart-work not hardwork

def is_int(x):
if abs(x-round(x))>0:

    return False
    return True


the elif x-round(x) part confused me, but after trying it out in an interpreter, I realized that you mean x minus round(x).