2

This is really strange, please take a loop at code snippet below, when i remove num=i, all works fine but with it, it is causing iteration issue.

Error code

In [17]: prim_list=[]
    ...: for i in range(2,101):
    ...:     print i
    ...:     num=i,count=0        # line with issue
    ...:     for j in range(1,i/2):
    ...:         div=j
    ...:         if(num%div==0):
    ...:             count=count+1
    ...:     if(count==1):
    ...:         prim_list.append(num)
    ...:
2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-17-a8dd2a52322e> in <module>()
      2 for i in range(2,101):
      3     print i
----> 4     num=i,count=0
      5     for j in range(1,i/2):
      6         div=j

TypeError: 'int' object is not iterable

Working fine

In [19]: prim_list=[]
    ...: for i in range(2,101):
    ...:     count=0              # notice num=i assignment is removed
    ...:     for j in range(1,i/2):
    ...:         div=j
    ...:         if(num%div==0):
    ...:             count=count+1
    ...:     if(count==1):
    ...:         prim_list.append(num)
    ...: print "done"
    ...:
done

What i am not understanding is why the assignment is causing the loop iteration error!!??

any help is appreciated!

3
  • 1
    num=i,count=0 is a single statement, meaning num=(i,count)=0. Unpacking assignment doesn't work with integers.
    – vaultah
    Jul 26, 2017 at 17:39
  • @MartijnPieters may be i put the question incorrectly but i am definitely trying to understand the wrong error it throws, how is python doing it internally that the assignment and error statement dont match!!
    – NoobEditor
    Jul 26, 2017 at 17:40
  • Duplicate of stackoverflow.com/q/16747599/2301450
    – vaultah
    Jul 26, 2017 at 17:52

2 Answers 2

6

You are using a chained assignment:

num=i,count=0

is

num = (i, count) = 0

Where num = 0 and (i, count) = 0 are assigned. Because 0 is not an iterable of length 2, it can't be assigned to a tuple of targets.

Use

num, count = i, 0

or

num = i
count = 0

instead.

2
  • 1
    ohh man...python never ends surprising with its simplicity..thanks, will accept in due time ( but in my defense, question wasn't that dumb to get 2 downvotes :D ) !
    – NoobEditor
    Jul 26, 2017 at 17:43
  • 1
    Or, num = i; count = 0 (note semicolon rather than comma, which makes it two separate statements)
    – kindall
    Jul 26, 2017 at 18:02
1

What is happening here is you are misinterpreting the comma operator. Unlike C, it does not allow you to do multiple statements. What it is doing is destructuring assignment, for example:

a, b = [1, 2]
# a == 1; b == 2

So, num=i,count=0 is more like:

num = i, count = 0
# equivalent to:
num = 0
i, count = 0

Which doesn't make sense.

The semicolon (;) does what you intended, but go with a newline:

num = i
count = 0

Not the answer you're looking for? Browse other questions tagged or ask your own question.