[Tutor] while x != 'n' or 'N' or 'l' or 'L':

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 6 Dec 2000 16:36:00 +0100


On Wed, Dec 06, 2000 at 09:12:48AM -0600, Brad Chandler wrote:
> > while ( string.upper( x ) not in ( 'N' , 'L' ) ) :
> > # body of loop here
> 
> I'm a beginner at all this, but I do think the above statement is much
> clearer. However, I don't think anyone has shown how the original statement
> should have looked. I know it helps me when I know why something doesn't
> work, and I think I also misunderstood the same thing at one time.
> 
> The original statement:
> while x != 'n' or 'N' or 'l' or 'L':
> 
> Should have been:
> while x != 'n' or x != 'N' or x != 'l' or x != 'L':

Yes. Your first version means

while (x != 'n') or ('N') or ('l') or ('L'):

to python. Any non-empty string is true, so 'N' counts as true, and you
have an infinite loop.

My personal favorite spelling is

while x not in 'N', 'n', 'L', 'l':

The commas make the four strings into a tuple, and all those ( ( ) ) and
spaces only confuse me :-).

-- 
Remco Gerlich