This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: An error in Python Tutorial
Type: Stage:
Components: Documentation Versions: Python 2.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, godhand, josiahcarlson, rhettinger
Priority: normal Keywords:

Created on 2005-05-29 15:28 by godhand, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (4)
msg25441 - (view) Author: Gene (godhand) Date: 2005-05-29 15:28
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. 
msg25442 - (view) Author: Josiah Carlson (josiahcarlson) * (Python triager) Date: 2005-05-31 06:22
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.
msg25443 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-05-31 09:31
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.
msg25444 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2005-05-31 09:36
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

History
Date User Action Args
2022-04-11 14:56:11adminsetgithub: 42029
2005-05-29 15:28:44godhandcreate