Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #79010
| References | <loom.20141012T190458-947@post.gmane.org> <CAPTjJmrPgTdysCnLcbMqYFiGGwg-wV_mjfw16Kt15_zuv_624g@mail.gmail.com> <loom.20141012T191849-946@post.gmane.org> |
|---|---|
| Date | 2014-10-13 04:31 +1100 |
| Subject | Re: while loop - multiple condition |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14796.1413135115.18130.python-list@python.org> (permalink) |
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: while loop - multiple condition Chris Angelico <rosuav@gmail.com> - 2014-10-13 04:31 +1100
csiph-web