Is_int

for me it looks liked you replyed to this
def is_int(x):
if type(x) == int:
return True
else:
return False

that the problem itself needs math model to be solved. probably just me not understand the forum format
anyways thanks for helping all of us just starting out, big props

1 Like

For my solution, I skipped importing the math module and just did some casting. by explicitly converting to an int, it should drop off any decimal.

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

I guess i was not thinking about the floor function (which would have made it a bit easier in hindsight). Same principal but more than one way to go about it i suppose.

This is the method I used. It passed but I hope it’s correct :slight_smile:

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

7 Likes

an alternative solution that doenst use the hint (the rounding tric), but that simply checks if there a decimal sign…

def is_int(x):
charss=str(x)
if ‘.’ in charss:
pointpos=charss.index(’.’)
seccar=charss[pointpos+1]
if seccar == ‘0’:
return True
else:
return False
else:
return True

hey i already passed the exercise but i don’t think i have done it correctly, i have read many topic about it and found many exercise where it said to import this or that and then i found your solution,
i knew what to do, in exerciser it is written exactly what to do, i.e > If the difference between a number and that same number rounded down is greater than zero
which mean the 9.22 - 9 < 0 then it is integer
but i actually didn’t knew how to do it,
thanks to you, i found the solution

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

1 Like

here check my solution as well :smile:
https://discuss.codecademy.com/t/is-int-problem/47951/5?u=ronit1710

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

If the difference between a number and that same number rounded down is greater than zero <=dis was in description

Here is my contribution!
def is_int(x):
if x == int(x) or x == int(-x):
return True
else:
return False

my version.

The directions said we could not check directly if a number was an int. Here we are converting the number to an int and checking to see if that result is the same as the raw number

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

this won’t work all the time.
any x that is a float under .5 ( i.e. 7.0 to 7.49) will pass this test.7.5 will round up in python.

@jlegere Simply a lie. Python support mixed arithmetics and comparisons. There is nothing wrong in comparing 2 with 2.00.

This:

print round(15) # => error: TypeError: a float is required

is just your imagination.


this won’t work all the time.

This will work all the time.

any x that is a float under .5 ( i.e. 7.0 to 7.49) will pass this test.7.5 will round up in python.

It does not matter.

  round(7.0) == 7.0
=> True
   round(7.39) == 7.39
=> False
   round(7.77) == 7.77
=> False
   round(8.0) == 8.0
=> True

Here we are converting the number to an int and checking to see if that result is the same as the raw number

Your solution is invalid. You should not use int on the x (input). And this is exactly what you did. Extra code doesn’t change that.

1 Like

head clearer now that it’s not 3am.

instructions: This means that, for this lesson, you can’t just test the input to see if it’s of type int.

I take this to mean I cannot use ```if type(x) == int``

int(x) == x is taking the integer portion of a number, whether there are extra digits or not and comparing it to the raw number.

would this not be correct?

also. the print round(15) # => error: TypeError: a float is required part, error was copied from console. not sure why it was giving me that.

withdrew the post since I am unable to recreate the error I was repeatedly getting last night.

I believe @factoradic has cleared that.

Thank you! :slight_smile:

1 Like

would this not be correct?

In this exercise, we are supposed to use arithmetic operations to check if given number is an int. int(x) is a type conversion, but you can cut off the fraction part using arithmetic operation. And you should do that. It’s my opinion, obviously.


also. the print round(15) # => error: TypeError: a float is required part, error was copied from console. not sure why it was giving me that.

In my opinion, this is a lie. But it does not matter. Just please check everything twice before stating that every other solution is wrong and before posting something on the forum for beginner programmers.

1 Like

My code:

numbers = [1, -2.0, 2.3, 3.4, 4, 4.5, 6, 7, 8, 9.5, -3.2, -3];

def is_int(x):
if abs(x) % 1 <= 0:
print x, “is an integer”
return True
else:
print x, “is a float”
return False

for x in numbers:
is_int(x)

1 Like

I used:

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

Thanks! This helped, silly me :stuck_out_tongue:

This was not that difficult as the directions and hints gave it all away.

Here was my code that passed:

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

And I did it yet another way! Which was accepted

import math

def is_int(x):
if x - math.trunc(x) == 0:
return True
else:
return False