Python Forum
'else' statement not executing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'else' statement not executing
#1
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:

#!/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
Reply
#2
Change

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()
Reply
#3
(Jul-19-2017, 08:30 PM)buran Wrote: eval(main_opt[choice - 1] + '()')

Thank you buran, that does the trick. I will try and use your preferred solution (using the dictionary) but that will take some time to reconfigure my script.

Thanks again.
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
Reply
#4
I am working on that tutorial you asked for sparkz, I'm almost finished with it. I hope to have it posted by the end of the week.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  if /else statement not executing correctly bluethundr 3 2,795 Apr-15-2019, 10:20 PM
Last Post: bluethundr
  if-elif-else statement not executing laila1a 4 3,037 Jan-05-2019, 02:22 PM
Last Post: buran
  print() statement not executing.. bmohanraj91 3 3,690 May-01-2017, 03:56 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020