2

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?

2rs2ts
11.2k12 gold badges56 silver badges100 bronze badges
asked Mar 4, 2014 at 2:59
2
  • 1
    It's often a better idea instead of taking the user input from the console in a interactive fashion to take it from the command line arguments instead (there are libraries to parse them -e.g. getopt- and they interact better -e.g. you can put it in a script, compose it with other programs-). All I'm saying is part of the Unix Philosophy en.wikipedia.org/wiki/Unix_philosophy Commented Mar 4, 2014 at 3:09
  • You could generate the menu structure programmatically from a config file e.g., in Windows ini format. The code Menu.from_config() is not big. To support more actions just define more Game.do_someaction methods. Context: Go to in Python 3 Commented Mar 4, 2014 at 17:45

4 Answers 4

2

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|...'
answered Mar 4, 2014 at 3:09
Sign up to request clarification or add additional context in comments.

Comments

0

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!

answered Mar 4, 2014 at 3:26

Comments

0

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.

answered Mar 4, 2014 at 3:08

Comments

0

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.

answered Mar 4, 2014 at 3:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.