[docs] Tutorial code is missing a break statement
philip at primacystudios.com
philip at primacystudios.com
Mon Aug 8 20:37:07 CEST 2011
Hello,
In section 4.4 "break and continue..." of the v2.7.2
tutorial documentation the loop iterates and returns a different output
than what is depicted.
Documentation printed 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 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'
My printed output:
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
I added a break statement to the else clause:
>>> for n in
range(2, 10):
for x in range(2, n):
if n% x == 0:
print n, 'equals',
x, '*', n/x
break
else:
print n, 'is a prime number'
break
To get
what is in the tutorial:
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
I hope this helps.
Phil Templeton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/docs/attachments/20110808/2b8f5825/attachment.html>
More information about the docs
mailing list