3/15 Practice Makes Perfect - Help


#1

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
else:
return False

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


#2

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!


#3

Can you please elaborate on this idea?


#4

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:


#5

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

OR

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

#6

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:

"string"
# or, with a number:
"1"

whereas an int looks like

1

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!


#7

@zystvan

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

EXAMPLE:

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.

#8

You don't have to call other functions:

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

#9

Thankyou, this helped me understand it better


#10

import math

n=input("Put your number: ")

is_int(n)

def is_int(x):
if (x - math.floor(x) == 0):
return True
print "This is a integer number!"
else:
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!


#11

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


#12

Heres the simplist way

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


#13

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


#14

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
    else:
        return True

#15

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


#16

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

7

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.


#17

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

Everyone else appears to add more than necessary.


#18

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


#19

i think this is smart-work not hardwork

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

    return False
else:
    return True

#20

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