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

Steve Holden sholden at holdenweb.com
Fri Apr 13 17:03:11 EDT 2001


"Ron Stephens" <rdsteph at earthlink.net> wrote ...
> Steve offered me this good code:
>
> choice = raw_input("Option: ")
> if choice == 1:
>     # code for case 1
> elif choice == 2:
>     # code for case 2
> elif choice == 3:
>     :
>     :
> else:
>     print "Invalid choice"
>
> Steve, thanks. I guess in this case, when the code for a choice finishes
> executing, program flow would go to (Alex: sorry about that particular
choice of
> words;-))) the end, or right after the print "invalid choice "
statement...?
>
After the particular case selected has executed control "drops off the
bottom" to whatever follows.

Of course you are at liberty to include in it a loop. By the way, those
integers I used should really be strings. So you could do something like:

while 1:    # loop forever
    choice = raw_input("Operation:")
    if choice == "this": # or maybe "1"
        # do this
    elif choice == "that": # or maybe "2", etc...
        # do that
    elif ...
        :
    elif choice == "quit":
        break    # exit from infinite loop
    else:
        print "Invalid operation"

This will loop, peforming the selected operations, until the user enters
"quit", or some exception terminates the run.

regards
 Steve






More information about the Python-list mailing list