Is_int


#1

How to solve the is_int part of pratctice makes perfect?


#2

Show us something you've tried. :slight_smile:


#3

Could you please copy and paste your code?


#4

I could only do until here.. I dont know other functions..

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

#5

Could I have a link to the exercise?


#6

Well, you need to import the math module.

import math

#7

https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096


#9

Quote from the exercise:

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

You have to change the condition of if. Here is a small hint:

6 % 1
# => 0

6 % 1
# => 0.0

6.45 % 1
# => 0.45000000000000018

7.12 % 1
# => 0.11999999999999922

You can also use the method proposed in the description. Simply check if given number is equal to this number rounded to integer :slight_smile:

round(12)
=> 12.0

round(12.0)
=> 12.0

round(3.66)
=> 4.0

round(2.353)
=> 2.0

#11

You don't even need to import the math module to complete this problem. The hint is a huge giveaway. Especially

n % 1 #where n is some arbitrary number

Your code is very, very close to perfect!


#12

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

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

print is_int(7.0)

Sorry to whoever seen this earlier, didn't have it formatted.


#13

it doesn't work for me...
it pops out "Oops, try again. Your function fails on is_int(-2). It returns True when it should return True", which is quite weird...It returns True when it should return True...

I have been driven crazy=.=


#14

Why what @blogcoder41425 won't work for you is that you need to import the math module

import math

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

Hope this helps! :slight_smile:

Please mind INDENTATION LEVELS.


#17

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

no importing and it pass the test


#18

Good it did.

Well, there was use of round method there and so there was need to import that the math module.

Thank you! :slight_smile:


#19

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


#20

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.


#21

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


#22

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


#24

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


#25

here check my solution as well :smile: