471,119 Members | 1,441 Online

# nested for loops

Can someone tell me why 'n' in this example isn't 2?
for n in range(2, 10):

for x in range(2, n):
print 'x =', x, 'n =', n
x = 2 n = 3
Mar 21 '06 #1
4 1147 On Tue, Mar 21, 2006 at 09:54:21PM +0000, John Salerno wrote:
Can someone tell me why 'n' in this example isn't 2?
>>> for n in range(2, 10):

for x in range(2, n):
print 'x =', x, 'n =', n
x = 2 n = 3

Assume n=2. Then the first inner loop will run in a range(2,2) which is an
empty list. So nothing will happen.

Next outer loop: n=3...

Actually n becomes 2. But you don't do anything with it. :)

Christoph
--
~
~
".signature" [Modified] 1 line --100%-- 1,48 All
Mar 21 '06 #2
John Salerno wrote:
Can someone tell me why 'n' in this example isn't 2?
>>> for n in range(2, 10): for x in range(2, n):
print 'x =', x, 'n =', n
x = 2 n = 3

Hint:
range(2, 2)

[]

Peter
Mar 21 '06 #3
It is, but range(2,2) doesn't do anything

Jesus Rivero - Neurogeek
John Salerno wrote:
Can someone tell me why 'n' in this example isn't 2?
for n in range(2, 10):

for x in range(2, n):
print 'x =', x, 'n =', n
x = 2 n = 3

Mar 21 '06 #4
Jesus Rivero - (Neurogeek) wrote:
It is, but range(2,2) doesn't do anything

Jesus Rivero - Neurogeek
John Salerno wrote:
Can someone tell me why 'n' in this example isn't 2?
> for n in range(2, 10):

for x in range(2, n):
print 'x =', x, 'n =', n
x = 2 n = 3

Thanks guys! I was thinking range(2, 2) gave you 2, for some reason!
Mar 22 '06 #5

### This discussion thread is closed

Replies have been disabled for this discussion.