5

Whats wrong with this code:

n = 10
((n/3)).is_integer()

I do not understand why I cannot set n = any number and check if it is an integer or not.

Thanks for your help!

python 2.7.4

error:

Traceback (most recent call last):
  File "/home/userh/Arbeitsfläche/übung.py", line 2, in <module>
    print ((n/3)).is_integer()
AttributeError: 'int' object has no attribute 'is_integer'
8
  • Traceback (most recent call last): File "/home/userh/Arbeitsfläche/übung.py", line 2, in <module> print ((n/3)).is_integer() AttributeError: 'int' object has no attribute 'is_integer' Commented Jun 18, 2013 at 13:35
  • 8
    Where did you get that is_integer() is a function? Try isinstance(n, int) Commented Jun 18, 2013 at 13:37
  • 4
    @Max probably from docs.python.org/2/library/stdtypes.html#float.is_integer Commented Jun 18, 2013 at 13:38
  • @Max thank you very much! I assume isinstance works for various things? Commented Jun 18, 2013 at 13:41
  • 2
    @frank Well, other than you've resurrected a 6 year old issue, because not all objects have an is_integer function. ints don't! Commented Jun 21, 2018 at 19:51

5 Answers 5

15

The reason you get this error is because you divide the integer 10 by 3 using integer division, getting the integral number 3 in the form of an int instance as a result. You then try to call the method is_integer() on that result but that method is in the float class and not in the int class, just as the error message says.

A quick fix would be to change your code and divide by 3.0 instead of 3 which would result in floating point division and give you a float instance on which you can call the is_integer() method like you are trying to. Do this:

n = 10
((n/3.0)).is_integer()
Sign up to request clarification or add additional context in comments.

Comments

6

You are using Python 2.7. Unless you use from __future__ import division, dividing two integers will return you and integer. is_integer exists only in float, hence your error.

Comments

3

the other answers say this but aren't very clear (imho).

in python 2, the / sign means "integer division" when the arguments are integers. that gives you just the integer part of the result:

>>> 10/3
3

which means that in (10/3).is_integer() you are calling is_integer() on 3, which is an integer. and that doesn't work:

>>> (3.0).is_integer()
True
>>> (3).is_integer()
AttributeError: 'int' object has no attribute 'is_integer'

what you probably want is to change one of the numbers to a float:

>>> (10/3.0).is_integer()
False

this is fixed in python 3, by the way (which is the future, and a nicer language in many small ways).

3 Comments

s/fixed/changed/ - both behaviours are reasonable.
@glglgl sorry, but no, i strongly don't agree. a language is a user interface. if it confuses many users it's bad.
no its not fixed, same behaviour in 3.10.
0

You can use isdigit, this is a good function provided by Python itself

You can refer documentation here https://docs.python.org/2/library/stdtypes.html#str.isdigit

    if token.isdigit():
        return int(token)

1 Comment

This answer only works if one is trying to determine if a string is an integer, so it doesn't appear to answer the question.
-2

...

When I wrote this answer there was no information about language.

But in python2 you can use the following to check if it's an integer or not

isinstance( <var>, ( int, long ) )

3 Comments

of course, but why do I get this error with is_integer: AttributeError: 'int' object has no attribute 'is_integer'
well, there is no such function for an int object...the error message says it.
Dv: in Python 2.7, / is integer division if applied to two integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.