3. is_int


#1




Oops, try again. Your function fails on is_int(-3.4). It returns True when it should return False.


Check for int


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

How do I check if it is an integer?


#2

Your function fails because you have engineered to only pass positive integers, you need to write up a condition that includes negative integers as well.


#3

Try to consider what an integer is as opposed to other forms of numbers.

Also consider that any number subtracted from itself will equal 0 and never be greater or less than

i.e.:

def is_int(7):
    if 7 - 7 > 0:
        return False
    else:
        return True

Your code will never return false because it will never be greater than 0.


#4

You might be able to piece it together knowing this:
When using the abs() code it will give you the absolute value (how far away the digit is away from 0 no matter if it's above or below 0)


#5

The long a drawn out method to test if a number is an integer:

# given some value for x
y = abs(x)    # greater than zero
while y > 0:  
    y -= 1
return y == 0

This is the basis of computer arithmetic. Adding and subtracting. If x is an integer y will come out as zero.

Since we cannot use the built in for this exercise,

return x - int(x) == 0

is not an option, but it is what the OP is reaching for.

Consider also that we can use modulo or floor division for this problem.


#7

I am stuck on this exercise as well, dont even know how to begin.

because 7.0 isnt considered a float in this exercise it says its a integer.

everything is throwing me off.


#8

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

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

also I tried this for negatives:

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

same error as above


#9

Does it not say in the instructions to consider 7.0 an integer?

For the purpose of this lesson, we'll also say that a number with a decimal part that is all 0s is also an integer, such as 7.0.

will always be zero. Modulo is remainder only, and can be applied to floats and integers, alike. When we use an integer as the modulus, such as 1, all that is left is the decimal remainder.

1.5 % 1 == 0.5

#10

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

The built-in function int(x) is a function that get the integer part from a number. such as int(7.2), we will get 7


#11

Which we all know. That is why it was a precondition that we not use the built-in function and derive our own algorithmic approaches to the problem. What other clever solutions can you come up with?


#12

The question preceding the exercise was a major hint I initially missed:

If the difference between a number and that same number rounded down is greater than zero, what does that say about that particular number?

I don't think we've been introduced to this yet, but there is a floor function in the math module that rounds down. Using that and a relational operator should help with the 'IF' statement.


#13

There is also an operator, so we don't need to call up the module.

x // n  =>  13.7 // 1 == 13

#14

Thanks! Only learned that after getting past the exercise though.


#18