Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #79039

Re: while loop - multiple condition

From Gelonida N <gelonida@gmail.com>
Subject Re: while loop - multiple condition
Date 2014-10-13 10:31 +0200
References <loom.20141012T190458-947@post.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.14815.1413189132.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 10/12/2014 07:08 PM, Shiva wrote:
> while ans.lower() != 'yes' or ans.lower()[0] != 'y':
>       ans = input('Do you like python?')

I personally consider double negations less intuitive than following:


while not( ans.lower() == 'yes' and ans.lower()[0] == 'y' ):

Reading this line yoy would have noticed as wellm that what you really 
wanted would have been:

while not( ans.lower() == 'yes' or ans.lower()[0] == 'y' ):



I would write the coder differently.

With your code you have to pre-initialze the variable ans.


I personally consider it also more 'intuitive' / easier to understand if
I can see the break conditiion. to many nots  / != / negations can be 
confusing as you noticed yourself.

Taking into account the Steven's suggestion about using the 'in' 
expression it could be:


while True:
     ans = input('Do you like python?')
     if ans.lower() in ('yes', 'y'):
         break





Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: while loop - multiple condition Gelonida N <gelonida@gmail.com> - 2014-10-13 10:31 +0200

csiph-web