[docs] Documentation Bug frm ActivePython User Guide

Kevin Smith ksmith1 at telus.net
Tue Apr 2 22:08:49 CEST 2013


             Hello,
    I tried the code from this section in the guide ...

4.4. break <../reference/simple_stmts.html#break> and continue 
<../reference/simple_stmts.html#continue> Statements, and else 
<../reference/compound_stmts.html#else> Clauses on Loops

The break <../reference/simple_stmts.html#break> statement, like in C, 
breaks out of the smallest enclosing for 
<../reference/compound_stmts.html#for> or while 
<../reference/compound_stmts.html#while> loop.

The continue <../reference/simple_stmts.html#continue> statement, also 
borrowed from C, continues with the next iteration of the loop.

Loop statements may have an else clause; it is executed when the loop 
terminates through exhaustion of the list (with for 
<../reference/compound_stmts.html#for>) or when the condition becomes 
false (with while <../reference/compound_stmts.html#while>), but not 
when the loop is terminated by a break 
<../reference/simple_stmts.html#break> statement. This is exemplified by 
the following loop, which searches for prime numbers:

Example code:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break
...     else:
...         # loop fell through without finding a factor
...         print n, 'is a prime number'
...

Output:

2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3

Here is the output that I got:

3 is a prime number
4 equals 2 * 2
5 is a prime number
5 is a prime number
5 is a prime number
6 equals 2 * 3
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
7 is a prime number
8 equals 2 * 4
9 is a prime number
9 equals 3 * 3
 >>>

As you can see, the number two was skipped over. I have discovered that 
the 1st iteration of the second for loop counting for x when n is 2 
yields an empty range list [].
Typing range(2,2) ...  (which is what the 1st iteration of both loops 
would yield for the second range list) ... in the shell outputs "[]" and 
so the program doesn't execute the
if statement. It just jumps back to the first for line for n and n goes 
to 3.
Then it seems to work til it gets to 9 where we can see that it thinks 9 
is a prime
I haven't yet figured out where that fault lies.
When I add lines to print n and x thru each iteration of each for loop, 
x doesn't count properly.

With this code here you cane see how the loops are counting. ( I have 
reformatted the output so as to see it easier.)

for n in range(2,10):
    print "n = ",n
   
    for x in range(2,n):
        print "x = ",x

OUTPUT:
n =  2
n =  3 x =  2
n =  4 x =  2 x =  3
n =  5 x =  2 x =  3 x =  4
n =  6 x =  2 x =  3 x =  4 x =  5
n =  7 x =  2 x =  3 x =  4 x =  5 x =  6
n =  8 x =  2 x =  3 x =  4 x =  5 x =  6 x =  7
n =  9 x =  2 x =  3 x =  4 x =  5 x =  6 x =  7 x =  8

Notice on the first iteration  of n = 2, x is not counted nor printed.
Because when range(2,2) is run it yields an empty list.
x always lags behind by 1 which is I think why it screws up at 9. 
Actually now if I remember right ... from the debugging process x starts 
miscounting after 8 again.
Anyway, I spent hours trying to figure this out.
Maybe the break staement is the cause ?

Kevin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20130402/1cdfb0a1/attachment-0001.html>


More information about the docs mailing list