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?/
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)
Okay I did it and my console window was like “SURPRISE!!! You’re one big dumbhead!”
Lil’ help here
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.
you can try it as
if x == int(x):
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:
if x == int(x):
Thanks also methadwikarina and tayuly
if x%1 == 0:
THIS ONE WORKS
if (x) - math.floor(x) <= 0:
if x==0 or (abs(x)/abs(math.floor(x))==1):
if x > 0 and x - round(x) != 0:
elif x < 0 and round(x) - x != 0:
This code works great!
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…
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
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