Jul-19-2017, 08:30 PM
Change
Of course you can always do
main_opt = ['opt1', 'opt2', 'opt3']to
main_opt = [opt1, opt2, opt3]and
main_opt[choice - 1] + '()'to
main_opt[choice - 1]()your code
main_opt[choice - 1] + '()'just creates a string that you don't use at all -i.e. you don't assign it to a variable.
Of course you can always do
eval(main_opt[choice - 1] + '()')Actually, I would do
#!/usr/bin/env/python3 def opt1(): print("in opt 1") return def opt2(): print("in opt 2") return def opt3(): print("in opt 3") return def main_mnu(): print('This is the Main Menu. Please make a selection.') print(' 1) opt1') print(' 2) opt2') print(' 3) opt3') main_opt = {'1':opt1, '2':opt2, '3':opt3} while True: choice = input('Enter choice: ') try: main_opt[choice]() break except KeyError as err: print('Not an option, try again.', err) if __name__ == '__main__': main_mnu()