classification
Title: Documentation Section 4.4
Type: compile error Stage:
Components: Documentation Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, lehmannro, str8lazy
Priority: normal Keywords:

Created on 2008-02-28 16:42 by str8lazy, last changed 2008-02-28 20:11 by str8lazy. This issue is now closed.

Messages (5)
msg63096 - (view) Author: Jarod (str8lazy) Date: 2008-02-28 16:42
Section 4.4 of the tutorial gives example 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'
... 
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

When one is to enter the code (print n, 'equals' x, '*' n/x) you are
given syntax error:   File "<stdin>", line 4
    print n, 'equals' x, '*' n/x
                      ^
SyntaxError: invalid syntax
msg63097 - (view) Author: Robert Lehmann (lehmannro) * Date: 2008-02-28 17:01
In the example code from the tutorial you gave, there was still a comma
separator between the string 'equals' and the reference `x`. This is
missing when you entered the code, that's why Python is throwing an
exception there.
msg63098 - (view) Author: Jarod (str8lazy) Date: 2008-02-28 17:47
I made a type in that line, but when the typo isn't there I get the same
thing. It turns out that it was an error from running a slightly older
version of the dev kit. Now that I have updated the dev kit it runs, but
I end up with the following as an 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

as opposed to the desired

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
msg63103 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-02-28 18:39
Sorry, but there must be another typo in your code.
I guess the else clause isn't correctly indented -- it belongs to the
"for" statement, not the "if" statement.
msg63105 - (view) Author: Jarod (str8lazy) Date: 2008-02-28 20:11
@Georg: you were correct, the indentation was incorrect, I suggest that
there be some additional notation be added to code examples in the
documentation showing how many tabs there are, to make for an easier
read and minimize on errors.
History
Date User Action Args
2008-02-28 20:11:39str8lazysetmessages: + msg63105
2008-02-28 18:39:45georg.brandlsetstatus: open -> closed
resolution: not a bug
messages: + msg63103
nosy: + georg.brandl
2008-02-28 17:47:25str8lazysetmessages: + msg63098
2008-02-28 17:01:45lehmannrosetnosy: + lehmannro
messages: + msg63097
2008-02-28 16:42:53str8lazycreate