[Tutor] Creating sub-menus?

Alan Gauld alan.gauld at btinternet.com
Wed Dec 31 10:10:45 CET 2008

"nathan virgil" <sdragon1984 at gmail.com> wrote

> Each menu is a function that prints out options, saves a raw_input 
> as the
> variable choice, and returns choice. In the main menu, each option 
> leads to
> a sub-menu. After choice is defined, however, the sub-menu "tags" 
> the value
> of choice.

Yes that can all work.

> Then create a loop of while choice !=q, run current_menu, and 
> include a
> bunch of statements along the lines of:
> if choice == <value that leads to first sub-menu>:
>        current_menu = <function name for first sub-menu>

Consider using a dictionary keyed by your combined choice values.
Then the big if/elif chain shrinks to

returnValue = FuncDict[choice](params)

The only challenge with this route is making all the functions
take a single input argument. But that argument can be a tuple :-)

> This seems like it would work, but for some reason, every time I run 
> the
> code, it freezes after I give input from the main menu. Can anybody 
> help? I
> can show my source code, but indentation doesn't seem to copy/paste 
> very
> well, so it may be a bit hard to read...

Try putting it on the pastebin web site and sending us the URL.
That gives us colour coding of syntax too which helps read it!


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list