Ezio Melotti <ezio.melo...@gmail.com> added the comment: "for j in range(1,5), i in range(120,500,12):" is equivalent to "for j in (range(1,5), i in range(120,500,12)):" so the sequence here is a tuple that contains range(1,5) as first element and "i in range(120,500,12)" as second element. Since 'i' is needed before the loop starts to check if it's in range(120,500,12), if you don't define it, it raises a NameError. If 'i' already had some value before, then the code works, but it doesn't do what you expect. I.e., this code: >>> i = 0 >>> for j in (range(1,5), i in range(120,500,12)): ... j,i ...
results in : (range(1, 5), 0) (False, 0) because the first element of the sequence is range(1,5) so at the first iteration j = range(1,5) and since 'i' is untouched it's still 0, so the first line ends up being (range(1,5), 0). At the second iteration the value of j is "i in range(120,500,12)" and since 'i' is 0 and 0 is not in range(120,500,12), that evaluates to False. So we have j = False and 'i' is still 0, hence the (False, 0) in the second line. The different results are because probably you had 'i = <something>' in your program but not in the interpreter. What you want is "for j, i in zip(range(packetlen+1), channels): ..." or possibly "for j, i in enumerate(channels): ...". I'm not sure anything can be done in this case, except maybe require () in case the sequence contains an 'in', but I'm not sure it's worth doing it. I'll leave this open for a while to see if other developers have any comment. ---------- nosy: +ezio.melotti status: open -> pending _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue9240> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com