Question

I am working on a script which provides the user a lot of options to choose from, e.g

Choose function Options

  1. A - Do A(Description)
  2. B - Do B(Description)
  3. C - Do C(Description)
  4. D - Do D(Description)

(If A is selected)Choose A Subfunctions

  1. A1 - Do A1(Description)
  2. A2 - Do A2(Description)
  3. A3 - Do A3(Description)

(If B is selected)Choose B Subfunctions

  1. B1 - Do B1(Description)
  2. B2 - Do B2(Description)
  3. B3 - Do B3(Description)

and so on...

Each option is available for the option to choose and based on the choice, the python program will proceed. Please note before each option I provide a print

  print ''' Enter Directory 
  1. Current Working Directory 
  2. CHoose other folder'''

Then I use raw_input to accept user inputs, which basically in this case means a lot of prompts for the user (and messy code) . I suspect this might not be the best design .

My question is am I doing it correctly or is there an alternative approach which I have missed out?

Was it helpful?

Solution 2

One option (ha!) is to store the structure of the options in a variable.

For example, (in your mind) define "a menu" as a dict which contains some subset of these values:

  • "question" - this is the question to ask when showing the menu - "Choose B Subfunctions"
  • "description" - this is the option you selected to get to this menu - for example, for the "Choose B suboptions", this would be "B" - the answer you chose to get here. The first menu won't have this.
  • "suboptions" - this is a list of of menus from here.
  • "code" - if this exists, rather than showing this menu, run this code.

for the example you gave above

def function_for_A1():
    print "You chose A1!"

menus={"question":"Choose function Options","answers":[
          {"description":"A","question":"Choose A Subfunctions","answers":[
              {"description":"A1","code":function_for_A1},
              {"description":"A2","code":function_for_A2},
          ]},
          {"description":"B","question":"Choose B Subfunctions","answers":[
              {"description":"B1","code":function_for_A1},
              {"description":"B2","question":"B2 sub-suboption","answers":[...]},
          ]},
}

Once you have this structure, given the current menu, displaying it is fairly easy - run the code (if it exists), otherwise display the question, and for each of the answers display a number and the "description". Once they select a number, find the menu, wash, rince, repeat. You could make the menu function recursive, so if they select "0 - back", then you return from the current function.

Done!

OTHER TIPS

one way is to provide command line input( argv )while user runs the function. The code can have if elif cases to execute specific blocks based on the user input.

For example something like below:

something like - if user runs the program as 'example.py argv[1]', argv[1] can be the internal function names like A,B ...

if sys.argv[1] == 'A':
    function_name = raw_input("enter A1/A2/A3":)
    func(function_name)

elif sys.argv[1] == 'B':
    function_name = raw_input("enter B1/B2/B3":)
    func(function_name)
else:
    print >> sys.stderr,'usage:example.py A|B|...'

You can create a dictionary with the keys being the user possible input and the value beinga pointer to the next available dictionary. Make the raw input a function that is called within the loop. Exit the loop at the appropriate point.

You could keep a dictionary for each "level" of selections/options, with functions as the value and the user input as the keys:

primary = {'A':opt_a, 'B':opt_b, 'C':opt_c, 'D':opt_d}
secondary = {'A1':opt_a1, 'B':opt_b1, 'C1':opt_c1, 'D1':opt_d1}

opt = raw_input("Choose - A, B, C, D - ")
opt_func = primary.get(opt)

if not opt_func:
    print "Invalid Option"
else:
    opt_func()

def opt_a():
    opt = raw_input("Choose - A1, A2, A3, A4 - ")
    opt_func = secondary.get(opt)
    if not opt_func:
        return "Invalid Option"
    opt_func()             

def opt_b()...

Then do the same for however many layers you are wanting to do with each option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top