Reputation: 3789
I am working on a script which provides the user a lot of options to choose from, e.g
Choose function Options
(If A is selected)Choose A Subfunctions
(If B is selected)Choose B Subfunctions
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?
Upvotes: 2
Views: 326
Reputation: 8292
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.
Upvotes: 0
Reputation: 5919
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:
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!
Upvotes: 0
Reputation: 434
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|...'
Upvotes: 2
Reputation: 4425
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.
Upvotes: 0