[Python-bugs-list] [ python-Bugs-424552 ] Example function # 4.4 is incorrect

noreply@sourceforge.net noreply@sourceforge.net
Mon, 21 May 2001 09:57:07 -0700


Bugs item #424552, was updated on 2001-05-16 08:11
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=424552&group_id=5470

Category: Documentation
Group: None
Status: Closed
Resolution: Invalid
Priority: 5
Submitted By: Nobody/Anonymous (nobody)
Assigned to: Fred L. Drake, Jr. (fdrake)
Summary: Example function # 4.4 is incorrect

Initial Comment:
(Refers to the online tutorial at 
http://www.python.org/doc/current/tut/node6.html#SECTIO
N006400000000000000000)

The code should be changed to:

>>> for n in range(2, 10):
...     for x in range(2, n):
...         if n % x == 0:
...             print n, 'equals', x, '*', n/x
...             break 
...         elif x < (n-1) :
...             continue # keep checking
...         else:
...             print n, 'is a prime number'



Otherwise, you will get multiple "7 is a prime number" 
statements with the current code. Also, if would 
incorrectly say "9 is a prime number" and "9 equals 3 
* 3". I added the elif/continue part to take care of 
these two problems.



----------------------------------------------------------------------

>Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-05-21 09:57

Message:
Logged In: YES 
user_id=3066

Checked in as part of Doc/tut/tut.tex revision 1.135.

----------------------------------------------------------------------

Comment By: Fred L. Drake, Jr. (fdrake)
Date: 2001-05-16 08:41

Message:
Logged In: YES 
user_id=3066

I'll add a comment to the "else" clause of the for loop in  a future checkin, but the code is correct.

(The submitter admitted he made an indentation error in a followup email.)

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=424552&group_id=5470