Jul-19-2017, 08:22 PM
I've been scratching my head for quite awhile on this. The problem being the 'else' statement doesn't run, or at least doesn't get me to "def opt2():" though the print function I added shows that the option should run. Am I missing something? Obviously I am, but if it's simple, please make it sound complicated so I don't feel stupid.
The code:
The code:
#!/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')
opt_num = [0, 1, 2]
main_opt = ['opt1', 'opt2', 'opt3']
while True:
try:
choice = abs(int(input('Enter choice: ')))
if choice == min(opt_num) + 1:
opt1()
break
elif choice == max(opt_num) + 1:
opt3()
break
else:
main_opt[choice - 1] + '()'
print(main_opt[choice - 1] + '()')
break
except (IndexError, ValueError) as e:
print('Not an option, try again.', e)
continue
if __name__ == '__main__':
main_mnu()The result:Output:C:\Python\test.py
This is the Main Menu. Please make a selection.
1) opt1
2) opt2
3) opt3
Enter choice: 1
in opt 1
C:\Python\test.py
This is the Main Menu. Please make a selection.
1) opt1
2) opt2
3) opt3
Enter choice: 3
in opt 3
C:\Python\test.py
This is the Main Menu. Please make a selection.
1) opt1
2) opt2
3) opt3
Enter choice: 2
opt2() # result of print function, but not getting to the 'def opt2():'Thanks for any insight.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
