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


Groups > comp.lang.python > #79010 > unrolled thread

Re: while loop - multiple condition

Started byChris Angelico <rosuav@gmail.com>
First post2014-10-13 04:31 +1100
Last post2014-10-13 04:31 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: while loop - multiple condition Chris Angelico <rosuav@gmail.com> - 2014-10-13 04:31 +1100

#79010 — Re: while loop - multiple condition

FromChris Angelico <rosuav@gmail.com>
Date2014-10-13 04:31 +1100
SubjectRe: while loop - multiple condition
Message-ID<mailman.14796.1413135115.18130.python-list@python.org>
On Mon, Oct 13, 2014 at 4:21 AM, Shiva
<shivaji_tn@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.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web