_spam_sux_tdfunk at _spam_sux_nettally.com
Fri Mar 24 05:25:29 CET 2000
In an article posted Fri, 24 Mar 2000 02:57:44 +0000,
Peter Bittner (bittneph at aston.ac.uk) said:
> I'm missing a 'do-while' statement in Python.
Alx and Charles are both correct, but I recently discovered something
that makes this approach problematic: how to get out of nested loops. In
cases where I'm nesting loops, I've taken to using something like this:
from exceptions import Exception
"Used to break out of nested loops"
commands = get_cmd_input()
for cmd,arg in commands:
if cmd == "quit":
raise Break, "Command processor terminated"
elif cmd == "#":
print "ignored: " + arg # get more commands
elif not valid_cmd(cmd):
print "invalid: " + cmd # get more commands
status = exec_cmd(cmd, arg)
if status == unrecoverable_error:
# can't continue
raise Break, "error: %s %s" % (cmd, arg)
except Break, msg:
This approach will let you break out of any number of nested loops --
though anything deeper than about four or so will likely lead to nesting
function calls for readability. The alternative is to set some condition,
test it in the inner loop, break out of that loop and test it again in
the next-outer loop and the next, and the next....
Fwiw, I'm missing a multi-way branch statement (switch/case, select/case,
etc.). Small price to pay....<g>
-=< tom >=-
Thomas D. Funk | "Software is the lever
Software Engineering Consultant | Archimedes was searching for"
Advanced Systems Design, Tallahassee FL. |
More information about the Python-list