[Tutor] calculator

alan.gauld@bt.com alan.gauld@bt.com
Wed, 29 Aug 2001 11:56:25 +0100

> suggestions you've received so far, is to code your main menu 
> (and in fact all of your menus) similar to this:

And I'll add some more menu maintenance code...

> menuoptions={'1':circlearea, '2':squarearea, '3':rectanglearea,
> '4':squareroot, '5':addnumbers}

def getMenuItem():
    shape = 0
    while shape not in '12345':   # validate the choice
      print "please choose an option"
      print "1. circle area"     # or use loop/dictionary trick here
      print "2. square area"
      print "3. rectangle area"
      print "4. square root"
      print "5. add numbers"
      shape = raw_input("> ")     # safer than input()
    return shape

choice = getMenuItem()

If you combine that with the trick of printing out the 
menu choices from the dictionary and pass the dictionary 
into the getMenuItem function then you have a reusable 
menu function... 

However ISTR that Python already provides stuff for doing 
all of this in the cmd module... :-)

Alan g.