Is Int


#1



https://www.codecademy.com/courses/python-intermediate-en-rCQKw/0/3?curriculum_id=4f89dab3d788890003000096


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


I am confused because I need for the code to tell me what is an integer. I tried using its hint that says, If the difference between a number and that same number rounded down is greater than zero, what does that say about that particular number?" but it didn't help. Help! thanks


Replace this line with your code. 
import math 
def is_int(x): 
    if int(x-x) < 0:
        print ("True")
    else:
        print ("False")


#2

You may have to return true (no quotes) or return false (no quotes), instead of printing. You wouldn't use quotes because you want to return a boolean value (true/false) not a string value.


#3

You need to use the round() function in your answer, I believe.

If x is the same as x rounded, then it must be a whole number (an integer)...


#4

Oh yeah, that too :grin: how did I not notice that.
shows how rusty I am after being gone for a bit!


#8

Does it say in the instructions to NOT use int()? There is one approach given in the Hint, I believe, which uses the modulo:

if x % 1 == 0:

The other suggestion, round() is also applicable, though again, it is a built-in function.

if x - round(x) == 0:

When the instructions do not specify an if..else statement, we may return the expression directly, without using if.

return x % 1 == 0

The expression is evaluated and returns True or False.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.