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

Ron Stephens rdsteph at earthlink.net
Fri Apr 13 16:43:11 EDT 2001


Alex Martelli wrote:


>
> OK.  Now, is each of these programs in a separate .py file, or
> is each a function, each with a different name, inside the same
> main .py file?
>

...yes, right now they are in separate .py files. I will try this solution.

Later, Alex offers this super code....

>
> while 1:
>     input_string = raw_input(prompt)
>     try:
>         input_code = int(input_string)
>     else:
>         if 1<=input_code<=num_programs:
>             function = functions[input_code-1] + '.py'
>             function()
>             break
>
> > 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???
>
> You don't really "jump" in this approach -- you do _invoke_ your
> code (in a file or function), but, when that is done (if without
> raising exceptions), you get back to the point from where you
> had invoked it.  Which is why I have the "break" statement, to
> end the 'while 1' loop when the operation of the subprogram
> is finished.  (If errors were to be expected and caught, the
> function-call would be placed inside a try/except statement).
>
> Alex

Thanks Alex!!! I also am looking right now at your Python cookbook web
site...wow this is a really great resource, thanks to you from me and from all
Python learners and users future and present!!!

I guess in the case of the while loop above,  I could nest the while loop in a
one bigger while loop that ask "do you want to run another program option
quit?"  Or, alternatively, and perhaps better, I could imbed a statement
asking that question after the else code, immediately before the "break"
statement, asking the same question. That would be better, I think...

Thanks!!!!





More information about the Python-list mailing list