BASIC or Python?

Peter Hansen peter at engcorp.com
Tue Jul 10 01:59:51 EDT 2001


Rainy wrote:
> 
> On Tue, 10 Jul 2001 02:17:31 GMT, Gerry Quinn <gerryq at indigo.ie> wrote:
> > In case anyone is fooled, here's an example from the tutorial at
> > www.python.org, which also has a FAQ on what Python is for:
> >
> >>>> for n in range(2, 10):
> > ..     for x in range(2, n):
> > ..         if n % x == 0:
> > ..            print n, 'equals', x, '*', n/x
> > ..            break
> > ..     else:
> > ..          print n, 'is a prime number'
> > ..
> >
> > Now some people may very well find that lovable, but I certainly don't.
> > Note how the clever language design means that the 'else' must be
> > level-indented with the 'for'.   
[snip]
> 
> Isn't this an example of how python's indentation is a *good* thing?
> 
> In python, above code causes an error, a programmer goes and corrects it.
> No harm done.

Actually, harm would be done.  The above code is actually correct
Python, since some loop constructs such as for and while can take
an 'else' clause which executes under certain conditions.

Output if a programmer "corrects" the above:

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

Output of the uncorrected version:

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

On the other hand, using this code as an example of why
indentation is a Bad Thing is silly, since most languages
against which Python is compared in discussions of block 
delimiters do not have this kind of 'else' clause...
 
-- 
----------------------
Peter Hansen, P.Eng.
peter at engcorp.com



More information about the Python-list mailing list