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


Groups > comp.lang.python > #79010

Re: while loop - multiple condition

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'operator': 0.03; 'doing?': 0.09; 'happen.': 0.09; 'subject:while': 0.09; 'cc:addr:python- list': 0.11; '"or"': 0.16; 'ans': 0.16; 'boolean': 0.16; 'evaluated.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'means.': 0.16; '{!r};': 0.16; 'wrote:': 0.18; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'mon,': 0.24; 'cc:2**0': 0.24; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; "doesn't": 0.30; 'programming.': 0.30; 'message- id:@mail.gmail.com': 0.30; '13,': 0.31; 'this.': 0.32; 'basic': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'expect': 0.39; 'either': 0.39; 'break': 0.61; "you're": 0.61; 'first': 0.61; 'different': 0.65; 'skill': 0.68; 'evaluate': 0.72; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=pq5leOaEHdlUljQ1DpA4oBOqH5ycxVgrww4IM1ufaVs=; b=iIzbgET+jX535BQf6KBQneJMKOWOzK8CiyY7bu36bQWzoetAKd2uVdMBQ6Y/JDes+c znIh4BKRBhHeUvqPCmAuc83F8C0ju4vq65gN0oUaZ3HTWmdt/TWQZ9v0sYIAj+OsrZ2U oSCrZTYPoQOpFA2ebf+YAzyiJVrGnQp+hrEatEEGdqpFZ7/heVIf77MIisG8nJukHiN6 4zgCFmcmO9fqBtcEWULAtvX/whgL4sAzh8CqyS4dG2wzg5ggMmkgZoEE9SBYlseJxUHE qHrjvrQVRkeELkuxPDgZhF/JiHXWttnV93jtEHtS1JcXdSjrT5aF8WlnJ4HQfhVJqAtW 1hrw==
MIME-Version 1.0
X-Received by 10.50.176.202 with SMTP id ck10mr24563466igc.2.1413135107106; Sun, 12 Oct 2014 10:31:47 -0700 (PDT)
In-Reply-To <loom.20141012T191849-946@post.gmane.org>
References <loom.20141012T190458-947@post.gmane.org> <CAPTjJmrPgTdysCnLcbMqYFiGGwg-wV_mjfw16Kt15_zuv_624g@mail.gmail.com> <loom.20141012T191849-946@post.gmane.org>
Date Mon, 13 Oct 2014 04:31:47 +1100
Subject Re: while loop - multiple condition
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.14796.1413135115.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1413135115 news.xs4all.nl 2933 [2001:888:2000:d::a6]:43572
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:79010

Show key headers only | View raw


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


Thread

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

csiph-web