[Tutor] While and for loops

Todd Matsumoto tmatsumoto at gmx.net
Tue Jul 14 09:14:07 CEST 2009


Okay,

I'm not sure if this is good practice, but I could assign a variable within the while loop, that is assigned something that will then break the outer loop.

while True:
    breakout = True
    <do something>
    for i in items:
        if i > 10:
            breakout = False
        else:
            <do something>
    if break is False:
        break

Is the above do-able? Is there a better way?

T
-------- Original-Nachricht --------
> Datum: Tue, 14 Jul 2009 00:05:30 -0700
> Von: wesley chun <wescpy at gmail.com>
> An: Todd Matsumoto <tmatsumoto at gmx.net>
> CC: tutor at python.org
> Betreff: Re: [Tutor] While and for loops

> > Can you run for loops in while loops and if yes, why did my if condition
> not break the loop?
> >
> > I read that loops sort of have an order of precedence, does that have
> anything to do with this problem?
> 
> 
> todd,
> 
> welcome to Python! you're right in that your questions are related to
> each other because the answer to both of your questions is the same:
> you can only break out of the innermost loop (from where you have your
> break statement). if you need to break out of the outer one, you need
> another break statement not contained in another loop.
> 
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>     http://corepython.com
> 
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com

-- 
Neu: GMX Doppel-FLAT mit Internet-Flatrate + Telefon-Flatrate
für nur 19,99 Euro/mtl.!* http://portal.gmx.net/de/go/dsl02


More information about the Tutor mailing list