3. is_int


#1

whats wrong in my code ?

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

#2

What's the type of 5.0?


#3

Hi @puneetsinha ,

Because the code that you posted is not formatted, it is difficult for other users to read and debug it. After code has been pasted into the editing window for posting, you can format it by selecting it, and then by clicking the </> button above the editing area. Alternatively, you can place three backquotes on the line before the code and three backquotes on the line after the code. This will enable us to see important details, such as the indentation and underscores. If you use the backquotes, your code will be color-coded, making it especially easy to read.

This is not the correct test to use for this exercise ...

if type(x)==int:

In fact, the instructions state ...

... for this lesson, you can't just test the input to see if it's of type int.

For this exercise, any number, including a float, that has the value of a whole number qualifies for a return of True. So, a number such as 8.0 would conform to that criterion.

The correct test to apply is one that determines if x is equivalent in value to int(x).


#4

Your if statement should be -i think-
if x == int(x):

I might be mistaken, but that how i got it to accept.


#5

You can also just use a bit of clever mathematics to determine if it is an integer or not.

My solution was to run x through:

x%1 == 0

Anything with a remainder of 0 would be an integer.


#9

I came up with this. My theory was to mod x and if it returns 0 or 1 without any decimals then it is an integer. If it returns .14 or 1.14 then it is a float. I tested it with several inputs and it worked.

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

#10

A method is_integer() will return true for 7.0 and false for 7.5, that was how I did mine. You can try the method too. Cheers


#11

You also can include a 'elif' to check negative numbers. There is a post that someone is working with the same alternative like you:


#12

the post asked the result of the difference between a number and that same number which makes it confusing.
defining a function which produces an integer by dividing x/x as an instance of our function......is that right?


#13

for me this worked perfetcly

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


#14

Hi everybody! That's my sokution for this task:

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

Here I was looking for a any decimal numbers, if they are


#15

Don't forget the e after fals


#16

Thank you, really helpful!


#17

alternative way:
from math import floor

def in_int(x):
x += .0
y = floor(x)
if x == y:
return True
else:
return False

--Jeffrey A.


#18

Another way

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

print (is_int(7.0))


#19

Another way, although I seem to have a very different approach. Maybe because learning this ■■■■ is turning my brain into applesauce!

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


#20