0

I can't seem to get my head around this. I would appreciate some help. It looks to me like the for function is iterating at a rate of n+1 for every new iteration, although I can't figure out why.

Here is my Python code:

for n in range(2, 10):
    print "This is %d" % n
    for x in range(2, n):
        print "%d" % n

And here is the output in Powershell:

This is 2
This is 3
3
This is 4
4
4
This is 5
5
5
5
This is 6
6
6
6
6
This is 7
7
7
7
7
7
This is 8
8
8
8
8
8
8
This is 9
9
9
9
9
9
9
9

Why is this happening?

5
  • range accepts three arguments. range(start, stop, step). u are passing start and stop. so its printing what it should be Commented Jan 28, 2015 at 12:39
  • @Raz debug code and see how it works step by step. You will get much more information. Commented Jan 28, 2015 at 12:40
  • I don't have a debugger. What is that? Commented Jan 28, 2015 at 12:40
  • @Raz debuger is an utility which allows you to run your code line-by-line. And on each step you can see what variables contain. Google it. Commented Jan 28, 2015 at 12:43
  • I will. cheers Alexander. Commented Jan 28, 2015 at 12:43

3 Answers 3

1

The end value for a range is not included, so the range from 2 to 2 doesn't include any values. You then create a range from 2 to 3, which only includes the value 2, etc.

As such, you are producing loops that'll loop n - 2 times, where n loops from 2 to 9 inclusive, and each inner loop loops from 2 to n - 1, inclusive.

For each nested loop, you are printing n again, which doesn't change in the inner loop. Try printing x instead, or better still, print it together:

for n in range(2, 10):
    print "This is %d" % n
    for x in range(2, n):
        print "n=%d, x=%d" % (n, x)

For each n this'll print the values for x from 2 to n - 1, inclusive:

>>> for n in range(2, 10):
...     print "This is %d" % n
...     for x in range(2, n):
...         print "n=%d, x=%d" % (n, x)
... 
This is 2
This is 3
n=3, x=2
This is 4
n=4, x=2
n=4, x=3
This is 5
n=5, x=2
n=5, x=3
n=5, x=4
This is 6
n=6, x=2
n=6, x=3
n=6, x=4
n=6, x=5
This is 7
n=7, x=2
n=7, x=3
n=7, x=4
n=7, x=5
n=7, x=6
This is 8
n=8, x=2
n=8, x=3
n=8, x=4
n=8, x=5
n=8, x=6
n=8, x=7
This is 9
n=9, x=2
n=9, x=3
n=9, x=4
n=9, x=5
n=9, x=6
n=9, x=7
n=9, x=8
Sign up to request clarification or add additional context in comments.

6 Comments

But why is 4 printed twice, and 5 three times, and 6 four times, etc?
Why is it looping n - 2 times?
4 is printed twice because you coded it. You have an inner loop for x in range(2, n): and if n is 4 it loops over the values 2 and 3. Did you want to print x instead?
@Raz: you are simply printing n. Did you mean to print x instead?
Martin I posted another answer to this addressed to you - I can't seem to fit it all in to this comments thread but if you scroll down you will see it.
|
0
for n in range(2, 10):
    for x in range(2, n):
        ...

You have nested loops. The upper bound of the inner loop is set to the current value of the outer loop.

The outer loop will iterate from 2 (incl.) to 10 (excl.):

  1. At first iteration, n is 2. So, the inner loop will iterate from 2 (incl.) to 2 (excl.). As the upper bound is in fact equal to the lower bound, the inner loop does not iterate.
  2. At second iteration of the outer loop, n is 3. So, the inner loop will iterate from 2 (incl.) to 3 (excl.). So there is one iteration of the inner loop with x = 2
  3. At third iteration of the outer loop, n is 4. So, the inner loop will iterate from 2 (incl.) to 4 (excl.). There is two iterations of the inner loop (x = 2 and the x = 3).

And so on...

Comments

0

I think I understand now. Within every single iteration of the first for loop, the second foor loop will iterate once for every 'x' value between 2 and n.

So if n is 7, the first for loop will iterate once for the number 7, and the second for loop will iterate once for every value between 2 and 7 (i.e. 5 times).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.