In the Absence of a GoTo Statement: Newbie needs menu-launcher to choose amongst sub-programs

Bob Kline bkline at rksystems.com
Fri Apr 13 16:14:10 EDT 2001


On Fri, 13 Apr 2001, Ron Stephens wrote:

> So, now I consider procedural menu program using raw_input. Still,
> how do I go to the appropriate sub-program when the user chooses
> one? With goto's this would be trivial.

Even more trivial without them.

import sys
def a(): print "processing using algorithm A"
def b(): print "processing using algorithm B"
def c(): print "processing using algorithm C"
def quit(): sys.exit(0)

cmds = { "A": a, "B": b, "C": c, "exit": quit }

while 1:
    cmd = raw_input("> ")
    if cmd not in cmds.keys(): print "unknown command %s" % cmd
    else: cmds[cmd]()

-- 
Bob Kline
mailto:bkline at rksystems.com
http://www.rksystems.com





More information about the Python-list mailing list