471,119 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

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.

Similar topics

25 posts views Thread by chad | last post: by
4 posts views Thread by dw | last post: by
46 posts views Thread by Neptune | last post: by
9 posts views Thread by Gregory Petrosyan | last post: by
5 posts views Thread by =?Utf-8?B?QUEyZTcyRQ==?= | last post: by
4 posts views Thread by toddlahman | last post: by
13 posts views Thread by Fredrik Lundh | last post: by
8 posts views Thread by Nathan Sokalski | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.