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

Doug Stanfield DOUGS@oceanic.com
Tue, 5 Dec 2000 22:41:56 -1000


> Hello everybody! 

Hi.

> Im new to both Python and programming, so you can expect me 
> to ask many
> questions in this list! ; )

This is a great list for exactly that.  We're all here to learn.
 
> Well... I made a (very simple) test module named prime.py 
> that basically
> checks if a number is prime (duh!). The module works fine by 
> itself, but
> now I am trying to make another script that will either tell 
> if a given
> number is prime or which numbers from a list are primes.
> I think theres a problem in the line that reads the same as 
> the title of
> this message, 

I think you're right.

> but I will include the whole script (sans the prime.py
> module, which is in the same directory):

That's always a good idea.  The other thing that will help a great deal is
to include the text of the traceback that you get when it doesn't run
correctly.  That often has most of the information needed to figure things
out.  I'm not trying any of what follows with Python, so there is room for
error here.  This is just from the top of my head.

> #!/usr/local/bin/python
> # Testing the module prime.py
> import prime
> print "This is a test script"
> x = ""
> while x != 'n' or 'N' or 'l' or 'L':
> 	x = raw_input ("Type 'L' for a list of primes or 'N' to 
> test a single
> number: ")

I'm just going to comment on this and skip the rest because it is what
you've asked about.  First the logic of your test is not consistant.  Python
needs four full comparisons.  When you write:
		x != 'n' or 'N' or 'l' or 'L'

Python may try to parse the first part and get it right, but can't figure
out the rest.  You're asking Python to make a decision based on x not equal
to the letter 'n', or the truth of the letter 'N', or the truth of 'l', or
'L'.  In other words, the last three are not being compared to x.  You may
be able to figure out how to correct this error from the above.

I'd suggest there is another way to do what you want.  It may or may not be
considered more 'Pythonic', but it seems to be more obvious when coded
(warning: untested code):

print "This is a test script"
x = ""
while 1:
    if x in ('n','N','l','L'):
        break
    x = raw_input ("Type 'L' for a list of primes or 'N' to test a single
number: ")

> Of course this is very very basic, but Im a bit stumped, and 
> I cant find
> anything in my Python book (Teach Yourself Python in 24hrs, 
> by Ivan Van
> Laningham) to help me out.

Thats a great book.  I enjoyed it a lot, and got a lot from it.  Hope I've
helped.

-Doug-