while loop - multiple condition

Tim Chase python.list at tim.thechases.com
Sun Oct 12 22:42:35 CEST 2014


On 2014-10-12 22:16, Marko Rauhamaa wrote:
> is equivalent with
> 
>     while ans.lower()[0] != 'y':
>          ans = input('Do you like python?')

And still better improved with

  while ans[:1].lower() != 'y':
    ans = input('Do you like python?')

in the event that len(ans)==0 (a situation which will crash the
various current examples).

It also only expends the effort to lowercase 0-or-1 characters,
rather than lowercasing an entire string just to throw away
everything except the first character.

-tkc






More information about the Python-list mailing list