[3.is_int] Why it's always wrong with my code?


#1

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

Here is my code to check if x is integer, however it returns always with such error:
"Oops, try again. Your function fails on is_int(7.0). It returns False when it should return True."

/*by the way how to insert a code formatted with 4 spaces?*/


#2

the last paragraph before the instruction will give you a clue:

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

Maybe you can use "elif" and compare the x with int(x)

:slightly_smiling:


#3

Okay I did it and my console window was like "SURPRISE!!!! You're one big dumbhead!"

Lil' help here :frowning:


#4

I just wanted to point out that in the instructions it states:

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.

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

That said... Combined with methadwikarina said, try a different method for checking if a number is an integer that compares the number and an integer check of that number in order to solve the problem.


#5

you can try it as
if x == int(x):
return true


#6

That's it.. so it's not through the function int() but I need to compare x with its integer number to pass this session..

I tried something like this and it works:

def is_int(x):
    if x == int(x):

Thanks also methadwikarina and tayuly


#7

if x % 1 == 0:
return True
else:
return False


#9
def is_int(x):
    if  x%1 == 0:
        return True
    else:
        return False

It works.


#10

THIS ONE WORKS

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


#11

import math
def is_int(x):
if x==0 or (abs(x)/abs(math.floor(x))==1):
return True
else:
return False

this work


#12
def is_int(x):
    if x > 0 and x - round(x) != 0:
        return False
    elif x < 0 and round(x) - x != 0:
        return False
    else:
        return True

This code works great!


#14

can someone explain why x % 1 returns the same value as abs(x) - abs(int(x)) ?
I don't understand how x % 1 is evaluated...


#15

There is a hint in the exercise "is_even", on the Practice Makes Perfect which explains:
"The modulo % operation is useful for determining if one number is divisible by another."

And there is a very didactic explanation, using Python, about modules in this Youtube video:
Advanced Arithmetic, Modulus and flor Division, from Computer Science UK


#16

When x is a whole number with a decimal point and a fractional value, say 3.14,

x = 3.14
print x % 1    # 0.14000000000000012

or to rid all the floating point effects,

print "%.2f" % (x % 1)    # 0.14

So we see that the opertion returns only the decimal fraction portion of a float. When the return is zero, the float is treated as an integer equivalent, hence, 1.0 % 1 == 1 % 1 returns True.


#18