[Tutor] TypeError: 'int' object is not iterable

Peter Otten __peter__ at web.de
Fri Jul 11 22:48:17 CEST 2014


Avishek Mondal wrote:

> Hi,
> 
> I wrote a simple program, as follows-
> def finddivisor(n1, n2):
> divisor = ()
> 
> for i in range(1, min(n1+n2)+1):
> if n1%i == 0 and n2%i==0:
> divisor = divisor + (i, )
> return divisor
> 
> n1 = eval(input('Enter first number: '))
> n2 = eval(input('Enter second number: '))
> 
> 
> divisors= finddivisor(n1,n2)
> print (divisors)
> 
> However, when I run it, the message
> 
> Traceback (most recent call last):
> File "C:/Python33/Trials/tuple trial.py", line 21, in <module>
> divisors= finddivisor(n1,n2)
> File "C:/Python33/Trials/tuple trial.py", line 12, in finddivisor
> for i in range(1, min(n1+n2)+1):
> TypeError: 'int' object is not iterable
> 
> shows up. Could you please tell me where I went wrong? Does it mean that
> if i in an integer, it will not be iterated? But isn't the code for i in
> range(1, n) a very frequently used line?
> 
> Thank you for your help!

Hint: the problem is not with range(), it's min() with a single argument:

>>> min(2,1)
1
>>> min(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable




More information about the Tutor mailing list