[Tutor] Input checking [letters or numbers]

Alan Gauld alan.gauld at freenet.co.uk
Sat Dec 24 15:53:28 CET 2005


> Another newbe question! I use "while True: " to evaluate an
> expression, I see that you used while 1: .. what's the diffrence if
> any?!

Python only provided boolean literal values (True, False) relatively 
recently.
Long time Python programmers, especially those with a C background(*)
are used to writing 1 to mean True. It's a hard habit to break. But it means
exactly the same as True and since True is usually more readable its 
probably
better to use that. (In theory Python could change to make 1 and True non
compatible, but in truth thats extremely unlikely because it is such a
deeply embedded tradition and huge amounts of existing code would break!)

(*)
Strictly speaking the numeric definition is based on 0 being False and True
being "non-zero". Thus C programmers used to do this:

#define FALSE   0
#define TRUE     !FALSE

This left the numeric value of TRUE up to the compiler, in most cases it 
would
be 1 but in some cases it could be -1 or very occasionally MAXINT (0xFFFF)
This led to strange behaviour such as:

if ((1-2) == TRUE) { // works in some compilers not in others!}

The correct way was to compare to FALSE or just use the result as a value:

if (2-1) {//this always works}

Pythons introduction of boolean type values should have gotten round all
that but unfortunately it doesn't:

>>> if (1-2): print 'True'
True
>>> if True: print 'True'
True
>>> if (1-2) == True: print 'True'
>>>

So unfortunately Pythons implementation of Boolean values is only
half True(sorry couldn't resist!:-). In a true boolean implementation
any non zero value should test equal to True...

No language is perfect!

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list