[Tutor] While and for loops

wesley chun wescpy at gmail.com
Tue Jul 14 09:19:38 CEST 2009


> 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?

this will work (once you fix the typo), but i prefer the previous
solution by A.T. since you're trying to do the same thing.

my original suggestion would've been to expand on the problem to find
a proper case to break out of each individual loop separately, i.e.,
you have a reason to break out of the inner one, and a different
reason for breaking out of the outer one.

but in your case, it looks like you're trying to break out of both due
to a single reason, and for that, A.T.'s solution is a more elegant
way to do what you just did above.

cheers,
-- 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


More information about the Tutor mailing list