[Tutor] Creating sub-menus?

nathan virgil sdragon1984 at gmail.com
Wed Dec 31 08:22:14 CET 2008


I was reading the Non-Programmer's Tutorial for Python, and became really
proud of myself when I realized I could create a menu for functions. I
decided to try to take this one step further and see if I could create not
just a menu, but a menu with <i>sub</i>-menus, too! Ultimately, the idea I
came up with is this:

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. Each sub-menu has it's own "tag", so that the program can tell
the first choice of sub-menu A from the first choice of sub-menu B. A
sub-menu function would end with code similar to:

choice = raw_input("What's your choice?")
choice = "a" + choice
return choice

Once I get all the menus and other functions out of the way, I get to the
part of the code that actually chooses which function to run. Here, I start
out with:

choice = "start"
current_menu = main_menu()

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>
elif choice == <value that leads to first non-menu function>
        <first non-menu function>

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...
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20081231/18964523/attachment.htm>


More information about the Tutor mailing list