3. is_int. Solved, but I want to take analysis from experienced


#1

Task:

An integer is just a number without a decimal part (for instance, -17, 0, and 42 are all integers, but 98.6 is not).

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.

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

My code:

def is_int(x):
    if type(x) == type(1):
        return True
    elif x - int(x) == 0:
        return True        
    else:
        return False

Task counted as completed right.
I just want to hear opinions.


#2

In brief form, this utility function can be made to return a boolean using the built-in type function and the class int:

def is_int(x):
    return type(x) == int

However this is not the objective of the exercise. Can you think of any other approach that does not use type or int?


#3

But, if make code like You did, numbers like 7.0 will be count as float, not integer, isn't it?


#4

That's one crux, and I bet the author made the float (decimal zero) a requirement to prevent people from submitting it.

There are two possible approaches that will both permit floats...

round()

%

I won't spoil it for you by spelling them out. If you have trouble, let us know.


#5

Thank You, is' enough to understand!:slight_smile:


#7