[Tutor] Fw: Creating sub-menus?

spir denis.spir at free.fr
Thu Jan 1 13:57:54 CET 2009


On Thu, 1 Jan 2009 10:58:44 +0000 (GMT)
ALAN GAULD <alan.gauld at btinternet.com> wrote:

> Forwarding to the list....
> Please use ReplyAll when responding.
> 
> 
> 
> 
> On Wed, Dec 31, 2008 at 4:10 AM, Alan Gauld <alan.gauld at btinternet.com> wrote:
> 
> "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 :-)
> 
> Dictionaries? Tuples? I just figured out functions, so I'm very new. I'm still working on
> understanding lists. I know what I have probably isn't the best solution for what I'm trying to
> do, but I'm trying to work with the little that I know. 
> 
> 
> 
> 
> 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!
> 
> 
> http://pastebin.com/m252eea7a

Below a copy of the menu control loop of your code, with some corrections.
denis

#In development
choice = "start"			# rather use None or "" as not-yet-setvalue
current_menu = main_menu()		# current_menu = main_menu : see below
while choice != "q":
      current_menu			# current_menu() : it's a call
      #Main Menu results
      if choice == "1":
         current_menu = temp_conv_menu()# idem: rather no '()' for consistency
      elif choice == "2":
           current_menu = area_menu()
      elif choice == "3":
           current_menu = perim_menu()
	### inside sub-menus, you simply forget to process "back to main menu" choices
	### in all active branches below, you also forget to go back up a menu level
	###	after execution of the terminal choice
	## so that, once reached, the user is stick in a terminal branch
      elif choice == "t1":
         temp = input("Celsius temperature: ")
         print "Fahrenheit:", celsius_to_fahrenheit(temp)
		* current_menu = temp_conv_menu / main_menu
		* [possibly with "x = raw_input("ok?")" before]
      elif choice == "t2":
        temp = input("Fahrenheit temperature: ")
        print "Celsius:", fahrenheit_to_celsius(temp)
	* elif "t3":
		* current_menu = main_menu
      elif choice =="a1":
         s = input("Width:")
         print "Square area:", area_s(s)
      elif choice =="a2":
         w = input("Width:")
         h = input("Height:")
         print "Rectangle area:", area_r(w, h)
      elif choice =="a3":
          none
      elif choice =="a4":
         r=input("Radius of circle: ")
         print "Circle area:", area_c(r)

In the sub-menus, I would also give a distinctive key for "back to main menu" (eg 'b' or 'm')
rather than a number. [all comments / changes / additions untested]

------
la vida e estranya


More information about the Tutor mailing list