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


Groups > comp.lang.python > #77599

Re: My backwards logic

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.038
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'else:': 0.03; 'clause': 0.09; 'python': 0.11; "'break'": 0.16; 'indent': 0.16; 'tends': 0.16; 'true:': 0.16; 'wrote:': 0.18; "python's": 0.19; 'result.': 0.19; 'input': 0.22; 'print': 0.22; '(a)': 0.24; 'instead.': 0.24; 'extension': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'lines': 0.31; 'accidentally': 0.31; 'sep': 0.31; 'fri,': 0.33; 'editor': 0.35; 'received:google.com': 0.35; 'too': 0.37; 'two': 0.37; 'level': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'worry': 0.60; 'break': 0.61; "you'll": 0.62; 'different': 0.65; 'relatively': 0.65; "'for'": 0.84; 'ethan': 0.84; 'furman': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=UVs1cNB7GXJVxg9kMDifDXApvpii8rWEVrvRdlsyUkA=; b=imKzq92b+5D6C/SxfsaQ0eqz4b/ORX7X+y3UbFtgaC1CFCNmuWqfLPy3WM1HrV40bC ConcCFGQh3otwTz3Vn3uwqCccVpWWr+w9UFyz29YU2aGvk859E++KiDfGhfby8vp5BZB AqpD5lD6rlsKXM1z3B5Y2MMls0/5+YgYQJQ9z15VkOYq+YtoCVWZ8uNYdqdYovYRJKJ8 cIlWbsdSSCASOZ59ymGqUzXmHPEwgn6NRoKns1wSc1kZKv+vMBXIdvuXT3+tomS6b7pq /DSOzhkv4isiZp4PFeamWgWla5tG+co2KvF8ZTBa7BjiB3vL6XHWCvVpHfjoO4ZiJtFl 8eqQ==
X-Received by 10.66.219.104 with SMTP id pn8mr22809179pac.133.1409937500821; Fri, 05 Sep 2014 10:18:20 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <5409EE02.7030308@stoneleaf.us>
References <1enj0att6bkrnvb81rhma5dbuk3h28agl8@4ax.com> <5409EE02.7030308@stoneleaf.us>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Fri, 5 Sep 2014 11:17:40 -0600
Subject Re: My backwards logic
To Python <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.13798.1409937511.18130.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409937511 news.xs4all.nl 2958 [2001:888:2000:d::a6]:49171
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77599

Show key headers only | View raw


On Fri, Sep 5, 2014 at 11:08 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
> Python's 'for' loop has a handy 'else' extension which is perfect for the
> search-type of 'for' loop:
>
>    while True:
>         a=random.randrange(1,8)
>         print (a)
>         for x in range(2,a):
>             if a%x==0:
>                 print ("Number is not prime")
>                 break
>         else:
>             print ("Number is prime")
>         wait = input (" "*40  + "Wait")
>
> Note the two lines I added after the 'break' and before the 'wait'.

Also note that this construct tends to be particularly sensitive to
indentation. If you accidentally indent the else block one level too
many (which your editor may well do for you to "helpfully" match it up
with the if), then you'll get a completely different result.

I would not worry about the else clause as a beginner, as it's
relatively unique to Python and tends to be somewhat confusing. Use a
flag or refactor the function instead.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 12:48 -0400
  Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 17:57 +0100
  Re: My backwards logic Bob Gailer <bgailer@gmail.com> - 2014-09-05 13:04 -0400
  Re: My backwards logic John Gordon <gordon@panix.com> - 2014-09-05 17:08 +0000
  Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 10:08 -0700
    Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 13:44 -0400
      Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 11:17 -0700
      Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 15:04 -0600
      Re: My backwards logic Chris Angelico <rosuav@gmail.com> - 2014-09-06 09:44 +1000
  Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 10:09 -0700
  Re:My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 13:15 -0400
  Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 11:17 -0600
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 14:35 -0300
  Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 11:41 -0700
  Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 19:48 +0100
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 16:34 -0300
    Re: My backwards logic Paul Rubin <no.email@nospam.invalid> - 2014-09-05 13:07 -0700
      Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:24 -0700
  Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-05 20:54 +0100
  Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 17:49 -0400
    Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 15:14 -0700
      Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 18:36 -0400
    Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 16:35 -0600
      Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 19:05 -0400
    Re: My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 23:10 -0400
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 22:54 -0300
    Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:58 -0700
  Posting style: interleaved responses (was: My backwards logic) Ben Finney <ben+python@benfinney.id.au> - 2014-09-06 12:37 +1000
  Re: Posting style: interleaved responses (was: My backwards logic) Juan Christian <juan0christian@gmail.com> - 2014-09-06 00:06 -0300
  Re: Posting style: interleaved responses (was: My backwards logic) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-09-05 23:04 -0500
  Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:45 +0100
  Re: My backwards logic Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-06 07:49 +0000
    Prime testing [was Re: My backwards logic] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-06 20:38 +1000
      Re: Prime testing [was Re: My backwards logic] Chris Angelico <rosuav@gmail.com> - 2014-09-06 20:42 +1000
      Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-06 12:53 +0200
        Re: Prime testing [was Re: My backwards logic] Peter Pearson <pkpearson@nowhere.invalid> - 2014-09-07 18:53 +0000
          Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-07 21:09 +0200
          Re: Prime testing [was Re: My backwards logic] Dan Stromberg <drsalists@gmail.com> - 2014-09-07 20:23 -0700
  Re: Posting style: interleaved responses Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:46 +0100

csiph-web