Is _int


#1

i keep getting this error
Oops, try again. Your function fails on is_int(-2). It returns False when it should return True.

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


#2

Does x equal to <type 'int'> (literally)? No. This is why your program is failing.

Now if you were to compare the type of x at least then there is a chance for a match. However, I don't think that is what was intended in this challenge.

Can we solve this without using any built-ins? Yes. Very simply, in fact, using the modulo (remainder) operator on unity (1).

Given a number that contains a decimal fraction (a float) we can divide by 1 and check for a remainder.

a = 1.61
print a % 1    # 0.61

#3

It said you can't use type() :stuck_out_tongue: Also I think the one thing that's screwing you up is the one thing that was holding me up. I don't know of the best way to hint at it without giving it away but what happens when you add a negative to a positive, versus adding a negative to a negative? :wink:

If that wasn't enough, it's mathematical symbol is | | as in |-12|


#4


#5

this is what i did:

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

1 tab before if x == int(x): 1 tab before else: and 2 tabs before return true and 2 tabs before return false


#6

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

Did you read the instructions? You are posting a solution we are told not to use. How does this help?


#8

This is kinda of a longer method of doing this thing...
Just tried to use string manipulation for the main checking method and used another method to check for invalid inputs:

num = raw_input('Enter a number: ')
def is_int(x):
    if is_number(x) ==True:
        y = str(x)
        if '.' in y:
            z =  y.index('.')
            print y[z+1]
            if y[z+1] == '0' and z+1 <= len(y):
                return True
            else:
                return False
        else:
            return True
    else:
        return 'Enter a real number'
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
print is_int(num)

#9

Treating the number as a string takes extra care. Your code is only checking the first digit after the decimal point. What if there are more digits, as in 12.00234.

num = raw_input('Enter a number: ')
def is_int(x):
    if is_number(x):
        if '.' in str(x):
            z = str(x)[str(x).index('.')+1:]
            if len(z) < 2 and z == '0': return True
            else: return False
        else: return True
    raise ValueError
    
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
        
print is_int(num)

Python is able to recognize truthy expressions, especially those that are already a boolean.

if is_number(x):

No need to make a comparison.


#10

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

print is_int(7.0)

simply comparing it to the rounded version of the number worked fine for me, fairly simple.