 # 3/15 Did I follow instructions correctly?

I’m not sure if I followed the instructions clearly:

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 int(x) != float(x):
return False
else:
return True
``````

I would also like to see some other correct ways of completing this exercise, thanks.

this is how i did it: I found your code to be a little extraneous. I solved it in a much simpler way:

``````import math
def is_int(x):
if x - math.floor(x) > 0:
return False
else:
return True``````

Here is my simple code:

2 Likes

If x were an integer as defined by the assignment, then wouldn’t x - math.floor(x) always be 0?
I did this way, same thing I think.

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

A matter of preference, but I’ve always preferred my if statements to be as exact as possible and let else catch the dirty stuff. This generally allows for easier modification of the code at a later time to account for other possibilities and expansion of functionality.

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

i did this way

mm… 5 Likes

def is_int(x):
print type(x)
if x != str(x) and x == int(x):
return True
else:
return False

print is_int(‘a’)
print is_int(7.0)
print is_int(7.5)
print is_int(-1)

how do u allready know math library functions ? you are not supose to , at this point of the tutorial

1 Like

looks fine to me… wouldn’t that check if the remainder from the modulus is zero? Meaning that if there was any decimal remainder the number was not an integer

print “Good job buddy”

you guys have it all wrong

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

this was all i needed to pass. workarounds shouldnt be this easy

5 Likes

Well, this discussion has been going on forever now, but one thing that keeps coming up is the if-else statement. There’s no need for one of those if it just is supposed to return True or False.

``````def is_int(x):
return x % 1 == 0
``````

And done edit:

Or we can get crazy and do something like this to catch some more non-int inputs without getting error messages:

``````def is_int(x):
try:
return x % 1 == 0
except:
return False

print "is_int(1) = " + str(is_int(1))
print "is_int(1.1) = " + str(is_int(1.1))
print "is_int(\"Batman\") = " + str(is_int("Batman"))
``````

I was wondering if using modulo would be the simplest way to get through this but i couldnt quite figure it out. Thank you for showing your code!

Awesome ! 2 lines of code!

Right code:

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

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

yup…it’s good code… thanks…

There are many ways to do this but its better to write less code.

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

Someone mentioned already that we are not supposed to be familiar with the math import function yet so I am guessing your code is the correct one fredilly?