How to solve the is_int part of pratctice makes perfect?
Show us something you've tried.
Could you please copy and paste your code?
I could only do until here.. I dont know other functions..
def is_int(x): if type(x) == int: return True else: return False
Could I have a link to the exercise?
Well, you need to
import the math module.
Quote from the exercise:
This means that, for this lesson, you can't just test the input to see if it's of type
You have to change the condition of
if. Here is a small hint:
6 % 1 # => 0 6 % 1 # => 0.0 6.45 % 1 # => 0.45000000000000018 7.12 % 1 # => 0.11999999999999922
You can also use the method proposed in the description. Simply check if given number is equal to this number rounded to integer
round(12) => 12.0 round(12.0) => 12.0 round(3.66) => 4.0 round(2.353) => 2.0
You don't even need to import the math module to complete this problem. The hint is a huge giveaway. Especially
n % 1 #where n is some arbitrary number
Your code is very, very close to perfect!
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 x == round(x): return True else: return False print is_int(7.0)
Sorry to whoever seen this earlier, didn't have it formatted.
it doesn't work for me...
it pops out "Oops, try again. Your function fails on is_int(-2). It returns True when it should return True", which is quite weird...It returns True when it should return True...
I have been driven crazy=.=
Why what @blogcoder41425 won't work for you is that you need to
import math def is_int(x): if x == round(x): return True else: return False is_int(7.0)
Hope this helps!
Please mind INDENTATION LEVELS.
no importing and it pass the test
Good it did.
Well, there was use of
round method there and so there was need to
import that the
for me it looks liked you replyed to this
if type(x) == int:
that the problem itself needs math model to be solved. probably just me not understand the forum format
anyways thanks for helping all of us just starting out, big props
For my solution, I skipped importing the math module and just did some casting. by explicitly converting to an int, it should drop off any decimal.
if(abs(x) - float(abs(int(x)))) > 0:
I guess i was not thinking about the floor function (which would have made it a bit easier in hindsight). Same principal but more than one way to go about it i suppose.
This is the method I used. It passed but I hope it's correct
if x % 1 == 0:
an alternative solution that doenst use the hint (the rounding tric), but that simply checks if there a decimal sign...
if '.' in charss:
if seccar == '0':
hey i already passed the exercise but i don't think i have done it correctly, i have read many topic about it and found many exercise where it said to import this or that and then i found your solution,
i knew what to do, in exerciser it is written exactly what to do, i.e > If the difference between a number and that same number rounded down is greater than zero
which mean the 9.22 - 9 < 0 then it is integer
but i actually didn't knew how to do it,
thanks to you, i found the solution
----if (x-round(x) )==0 :
here check my solution as well