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

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Jul 11 22:21:26 CEST 2014


On 11/07/2014 12:27, Avishek Mondal wrote:
> Hi,
>
> I wrote a simple program, as follows-
> def finddivisor(n1, n2):
>      divisor = ()

What's wrong with divisor = [] # a list

>      for i in range(1, min(n1+n2)+1):
>          if n1%i == 0 and n2%i==0:
>      divisor = divisor + (i, )

Then divisor.append(i)

>      return divisor
>
> n1 = eval(input('Enter first number: '))
> n2 = eval(input('Enter second number: '))

Please don't use the code above.

n1 = int(input('Enter first 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?

It is indeed, but not when you've mistyped it :)  Take a close look at 
your call to min()

>
> Thank you for your help!
>

Not at all.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com




More information about the Tutor mailing list