Tutorial code is missing a break statement

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
for n in range(2, 10): for x in range(2, n): if n% x == 0:
... 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: 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

On Mon, Aug 08, 2011 at 11:37:07AM -0700, philip@primacystudios.com wrote:
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.
Perhaps you did not indent your program correctly. The else goes with the for statement and not if. I tried the program from the text and it gives the output as documented. -- Senthil
participants (2)
-
philip@primacystudios.com
-
Senthil Kumaran