while loop - multiple condition

Gelonida N gelonida at gmail.com
Mon Oct 13 10:31:56 CEST 2014

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'):

More information about the Python-list mailing list