[Tutor] While loop issue, variable not equal to var or var

Alan Gauld alan.gauld at btinternet.com
Sat Jul 12 10:33:20 CEST 2014


On 11/07/14 22:16, Steve Rodriguez wrote:
> Hey guys n gals,
>
> New to python, having some problems with while loops, I would like to
> make a program quick once q or Q is typed, but thus far I can only get
> the first variable to be recognized.

 > My code looks like:
 >
 >      message = raw_input("-> ")
 >      while message != 'q':

Peter has given a comprehensive reply.

There are two other options he didn't mention.

1) Use 'and' instead of 'or':

while message != 'q' and message != 'Q':

2) Better (IMHO) is to convert message to lower case (or upper if
you prefer) and only do one comparison:

while message.lower() != 'q':


HTH,

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list