• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 295
  • Last Modified:

menu driven

hi i am creating a menu and when the user hits the keys, 1, 2 it does the following functions .....

1. Search
2. Exit

option = raw_input("Enter Your Option: ")

   optfunc = {

      '1': search(),
      '2': exit()
   }
   optfunc[option]()

it doesn't seem to work when i hit 1 or 2. Can anyone help thanks.
0
darrenteo82
Asked:
darrenteo82
1 Solution
 
darrenteo82Author Commented:
the exit function is

def exit():
   sys.exit()
0
 
RichieHindleCommented:
The code:

   optfunc = {
      '1': search(),
      '2': exit()
   }

actually calls the search and exit functions as it constructs the dictionary, and stores the result of calling the function in the dictionary (and of course sys,exit() never returns).  You need to omit the parentheses:

   optfunc = {
      '1': search,
      '2': exit
   }

This way, you're just referring to the functions rather than calling them.  Your line:

   optfunc[option]()

will then call the appropriate function as you expect.  Here's a full working example:

def search():
    print "search"

def exit():
    print "exit"

option = raw_input("Enter Your Option: ")

optfunc = {

      '1': search,
      '2': exit
   }

optfunc[option]()

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now