[ python-Bugs-1210832 ] An error in Python Tutorial

SourceForge.net noreply at sourceforge.net
Tue May 31 11:36:12 CEST 2005


Bugs item #1210832, was opened at 2005-05-29 10:28
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1210832&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Documentation
Group: Python 2.4
Status: Closed
>Resolution: Rejected
Priority: 5
Submitted By: Gene (godhand)
Assigned to: Nobody/Anonymous (nobody)
Summary: An error in Python Tutorial

Initial Comment:
In section 4.4, the program should be written as follow
to get the correct result:
--------------------------------------------------------------
for n in range(2, 10):
	for x in range(2, n):
		if n % x == 0:
			print n, 'equals', x, '*', n/x
			break
	if x == n-1:
		print n, 'is a prime number'
--------------------------------------------------------------
besides, the line "2 is a prime number" should not
appear in the result output. 

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

>Comment By: Raymond Hettinger (rhettinger)
Date: 2005-05-31 04:36

Message:
Logged In: YES 
user_id=80475

The code and output in the tutorial is correct.  Also, it
fulfills its purpose as an example of "else" and "break"
statements in a for-loop.

The OP's code contains an error.  It produces:
  NameError: name 'x' is not defined



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

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-05-31 04:31

Message:
Logged In: YES 
user_id=1188172

As for 2 being prime, Josiah is right.

As for your change to the code, it is equivalent to the one
in the tutorial. Besides, the sample code is there to
demonstrate the else clause on for/while loops, so maybe you
should look into this?

Closing as Invalid.

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

Comment By: Josiah Carlson (josiahcarlson)
Date: 2005-05-31 01:22

Message:
Logged In: YES 
user_id=341410

The indentation on your proposed code addition was lost
during your post, re-post so that it is understandable.

Further, from mathworld.com:
"A prime number (or prime integer, often simply called a
''prime'' for short) is a positive integer p > 1 that has no
positive integer divisors other than 1 and p itself. (More
concisely, a prime number p is a positive integer having
exactly one positive divisor other than 1.)"

That is to say, 2 is prime, so should appear in the result
output, and it seems to me that the code provided in
tutorial section 4.4 is correct.

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

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


More information about the Python-bugs-list mailing list