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


Groups > comp.lang.python > #37157

Re: Else statement executing when it shouldnt

Date 2013-01-21 01:09 -0500
From Mitya Sirenef <msirenef@lightbird.net>
Subject Re: Else statement executing when it shouldnt
References <2cc6791f-ba56-406c-a5b0-b23023caf4bb@googlegroups.com> <mailman.720.1358744227.2939.python-list@python.org> <11a77bd2-c537-4d0b-8af4-99d1f8c252d0@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.726.1358748565.2939.python-list@python.org> (permalink)

Show all headers | View raw


On 01/20/2013 11:59 PM, eli m wrote:
>
 >>
 >>
 >>
 >> Your else is lined up with while, not with if.
 >>
 >>
 >>
 >> -m
 >>
 >>
 >>
 >>
 >>
 >> --
 >>
 >> Lark's Tongue Guide to Python: http://lightbird.net/larks/
 >>
 >>
 >>
 >> When a friend succeeds, I die a little. Gore Vidal
 > Its lined up. It got messed up when i copied the code into the post.
 >

I would recommend using while True: and break vs. while var: as you
have. In most cases while True: works better, especially in case of long
and/or nested 'while' loops, as you have.

'while True' blocks have two advantages: 1. you can break the loop at
any location and 2. when looking at the code, you can tell on which
condition it breaks by looking at the break line.

Even more importantly, break it up into a few functions. The code as you
have it is too hard to work with and to debug.

It's hard to tell what your 'else' is lined up to, or whether some other
lines are mis-aligned, as well.

Generally, try to avoid making a loop if it's 20+ lines; if there are
nested loops, it makes things even worse. Compare:

if something:
     while True:
      if not process(): break

def process():
  [... 20 lines that loop ...]
  [ return None to break the loop ]

Now this is really clear, because just by looking at the first three
lines, I know what the loop is supposed to do (process something), that
it continues looping until it returns a false value; when looking at
the function body I don't need to care which block it aligns to, I
already know the entire function body is in the while loop.

HTH, -m



-- 
Lark's Tongue Guide to Python: http://lightbird.net/larks/

The irrational in the human has something about it altogether repulsive and
terrible, as we see in the maniac, the miser, the drunkard or the ape.
George Santayana

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


Thread

Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:40 -0800
  Re: Else statement executing when it shouldnt Roy Smith <roy@panix.com> - 2013-01-20 23:47 -0500
  Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-21 15:52 +1100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:55 -0800
  Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:54 -0800
    Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 22:00 -0800
      Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-21 08:04 +0000
  Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 05:54 +0100
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:06 +0100
      Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-21 06:07 +0100
        Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:39 +0100
          Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 02:42 +1100
            Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-22 16:48 +0100
              Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-23 03:07 +1100
              Re: Else statement executing when it shouldnt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-22 23:22 +0000
                Re: Else statement executing when it shouldnt René Klačan <rene.klacan@gmail.com> - 2013-01-23 01:34 +0100
                Re: Else statement executing when it shouldnt Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-01-22 22:11 -0500
              Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-22 17:28 -0800
                Re: Else statement executing when it shouldnt Thomas Boell <tboell@domain.invalid> - 2013-01-23 12:22 +0100
                Re: Else statement executing when it shouldnt Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-01-23 15:35 +0200
                Re: Else statement executing when it shouldnt Jerry Hill <malaclypse2@gmail.com> - 2013-01-23 09:53 -0500
                Re: Else statement executing when it shouldnt Frank Millman <frank@chagford.com> - 2013-01-25 10:15 +0200
          Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:48 +0000
            Re: Else statement executing when it shouldnt Duncan Booth <duncan.booth@invalid.invalid> - 2013-01-22 15:52 +0000
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:57 -0800
  Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-20 23:57 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt Mitya Sirenef <msirenef@lightbird.net> - 2013-01-21 01:09 -0500
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-20 20:59 -0800
      Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:46 -0800
  Re: Else statement executing when it shouldnt alex23 <wuwei23@gmail.com> - 2013-01-20 21:56 -0800
    Re: Else statement executing when it shouldnt eli m <techgeek201@gmail.com> - 2013-01-21 07:37 -0800
      Re: Else statement executing when it shouldnt Chris Angelico <rosuav@gmail.com> - 2013-01-22 05:44 +1100

csiph-web