3/15 Did I follow instructions correctly?


#1

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.


#2

this is how i did it:


#3

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

#4

Here is my simple code:


#5

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.


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

i did this way


#7

mm......... :sweat_smile:


#8

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)


#9

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


#10

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


#11

print "Good job buddy"


#12

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


#14

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 :stuck_out_tongue:

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

#15

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!


#16

Awesome ! 2 lines of code!


#17

Right code:

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

#18
import math

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

#19

yup...it's good code..:grin: thanks..


#20

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

#21

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?