while loop - multiple condition

Chris Angelico rosuav at gmail.com
Sun Oct 12 19:31:47 CEST 2014

On Mon, Oct 13, 2014 at 4:21 AM, Shiva
<shivaji_tn at yahoo.com.dmarc.invalid> wrote:
>> The loop will continue while either part is true - that's what "or"
>> means. Is that what you intended it to be doing?
>> ChrisA
> Yes......however, the second part of the or condition doesn't get evaluated.
> So if I enter a 'y' - I expect the second part to evaluate and the loop to
> break - but that doesn't seem to happen.

Break it down into separate parts and have a look at what's happening.

while ans.lower() != 'yes' or ans.lower()[0] != 'y':
    ans = input('Do you like python?')
    print("ans.lower() = {!r}; first cond = {!r}, second cond {!r},
disjunction {!r}".format(
        ans.lower(), (ans.lower() != 'yes'), (ans.lower()[0] != 'y'),
        (ans.lower() != 'yes' or ans.lower()[0] != 'y')

The while loop will continue so long as the disjunction is True. See
what it's actually doing.

This is fundamental Boolean logic, a basic skill of programming.
You're going to need to master this. Look at the different pieces, and
get your head around what the 'or' operator actually does.


More information about the Python-list mailing list