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'
tripleee
  • 175,061
  • 34
  • 275
  • 318
aaaa
  • 265
  • 1
  • 4
  • 9
  • Traceback (most recent call last): File "/home/userh/Arbeitsfläche/übung.py", line 2, in print ((n/3)).is_integer() AttributeError: 'int' object has no attribute 'is_integer' – aaaa Jun 18 '13 at 13:35
  • 8
    Where did you get that is_integer() is a function? Try isinstance(n, int) – Max Jun 18 '13 at 13:37
  • 4
    @Max probably from http://docs.python.org/2/library/stdtypes.html#float.is_integer – user247702 Jun 18 '13 at 13:38
  • @Max thank you very much! I assume isinstance works for various things? – aaaa Jun 18 '13 at 13:41
  • Reverted your "thanks" edit; please use upvotes to express gratitude instead. – tripleee Jun 18 '13 at 14:25
  • @tripleee I would if i could.... – aaaa Jun 18 '13 at 14:36
  • @Max is `isinstance(n, int)` better than `n.is_integer()` for some reason? Please clarify why you are suggesting this, thanks – Nathan majicvr.com Jun 21 '18 at 17:37
  • 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! – Max Jun 21 '18 at 19:51

5 Answers5

13

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()
Sebastian Ärleryd
  • 1,774
  • 14
  • 19
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.

Morwenn
  • 21,684
  • 12
  • 93
  • 152
4

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).

andrew cooke
  • 45,717
  • 10
  • 93
  • 143
-1

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)
Ravikiran Reddy Kotapati
  • 2,485
  • 3
  • 22
  • 27
  • 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. – Moot Apr 04 '18 at 17:24
-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 ) )
Ohlin
  • 4,068
  • 2
  • 29
  • 35
  • of course, but why do I get this error with is_integer: AttributeError: 'int' object has no attribute 'is_integer' – aaaa Jun 18 '13 at 13:34
  • 1
    well, there is no such function for an int object...the error message says it. – Ohlin Jun 18 '13 at 13:36
  • 1
    Dv: in Python 2.7, / is integer division if applied to two integers. – Max Jun 18 '13 at 13:37