Is_int


#1



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

The code executes fine, the problem is in the instruction box. It states "you can't just test the input to see if it's of type int."
So that means you cant use the function int(x) to compare. How can you check for an INT without using Boolean with the int() method?

I would like to see another example of how you can check for int with out using int() like I have. The lesson accepted the output and I was able to continue, but I feel like something is not correct because I used the int() method when the instructions said not to.

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


#2

Look up "isinstance", it works as well


#3

I originally used isinstance first and it didn't work:

def is_int(x):
      if isinstance(x, int) == True:
             return True
      else:
             return False

#4

this is what the information of the exercise says:
This means that, for this lesson, you can't just test the input to see if it's of type int

This refers to the bit above:
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

but int() will also consider 7.0 as integer. So, the information in the exercise seems to be faulty.

The information also 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?

Which would indicate you need to use something like: if x == math.floor(x):

But there are of course multiply ways to solve a problem


#5

So true.

Here follows one or two,

Python Code

2.* Python

def is_int(data):
    return '.' not in str(data / 1)

is_int(1)
# True
is_int(1.0)
# False
is_int('a')
# TypeError

# Fix for error
def is_int(data):
    try:
        return '.' not in str(data / 1)
    except TypeError:
        return False

is_int('a')
# False

You can replace the / with math.floor() for a version that can run on 2-4 with the same effect.

The reason this works is because we ensure that it is a number by dividing by 1 then we check to see if it contains a .


#7

While indisputable code, there is a tiny gotcha in this exercise:

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

so it raises this SCT message:

Your function fails on is_int(7.0). It returns False when it should return True.

That means using a modulo expression.

def is_int(data):
    try:
        return data % 1 == 0
    except TypeError:
        return False

or some equivalent method is needed to pass. Yours is a keeper, though. For sure.


#8

for this you can multiply the number by 10 and find the remainder of the number by dividing it by 10. if the remainder is 0 then it's still an integer.

def is_int(x):
if type(x)==int or (x*10)%10==0:
return True
return False


#9

A post was split to a new topic: Solve this problem using knowledge we have acquired so far