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

Paul Prescod paulp at ActiveState.com
Fri Apr 13 14:30:28 EDT 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. Now, I consider a nested set of "if "
> statements that determines which number was chosen, but still how do I
> "jump" to the correct sub-program??? Even if I define the subprograms as
> functions even, how do I jump to them???

Calling a function is the same as jumping to it. Now there are all kinds
of swank,y k-rad ways to solve your problem more efficiently or
compactly then what I describe below, but I'm trying to keep it simple
so you can learn at your own pace. 

def do_abc():
    "does some stuff"
    #...

def do_def():
    "does some stuff"
    #...

def do_ghi():
    "does some stuff"
    #...

while 1:
    command = raw_input("> ")
    if command == "abc":
        do_abc()
    elif command == "def":
        do_def()
    elif command == "ghi":
        do_ghi()
    elif command == "quit":
        break
    else:
        print "Unknown command"

-- 
Take a recipe. Leave a recipe.  
Python Cookbook!  http://www.ActiveState.com/pythoncookbook




More information about the Python-list mailing list