Python Forum
Help basic terminal menu
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help basic terminal menu
#1
Basically I want to get to start and pick a number and it go to the function specified. I know I haven't added mc or ex function yet but I can add them later. I have tried many things but im starting to think its time for help. If anyone can help and tell what im doing wrong it would be greatly appreciated.

To clarify further if I did do the functions correct Im not sure how I call them for the start function. So when I run for example it calls menu_choice function then goes to the appropriate function from the menu. So if I were to run it, It should pop up the menu asking "Choose your options..." I type in one for example "cc" it should then go to the cc function and let me finish on that function then when it spits out the cc print should ask if I would like to start again.

a = '''
*************************
*         menu          *
*************************
* CC - Character Count  *
* RV - Reverse          *
* M5 - Multiply by 5    *
* MC - Middle Characters*
*************************
* EX - Exit             *
*************************
'''
print(a)


def menu_choice():
  while True:
      choice = input("Choose your Options, CC, RV, M5, MC, EX :")
      if choice.lower() not in ('cc', 'rv', 'm5', 'mc', 'ex'):
          print("That is not a valid option.")
      else:
          return choice
          
def CC():
   if menu_choice == 'CC':
     cc = input('Enter string: ')
     print("'" + menu_choice + "'is", len(cc), "characters long")
     
def RV():
 if menu_choice == "RV":
   rv = input('Enter string: ')
   print(rv[::-1])
   
def M5():
 while True:
   if menu_choice == 'M5':
     m5 = input('choose "N" for number or "S" for string: ')
   if m5.lower() not in ('n', 's'):
     print('That is not a valid option.')
   elif m5.lower() == 'n':
     print(m5 * 5)
   elif m5.lower() == 's':
     print(str(m5) * 5)
   else:
     return m5

def start():
 
while True:
 start()
 restart = input('Do you want to go again? (y/n) ')
 if restart.lower() != 'y':
   break
Reply
#2
Use a dictionary, example:
def CC():
    print('This is funuction CC')

def RV():
    print('This is funuction RV')

MyFunctions = {
    'CC': CC,
    'RV': RV
}

def pick_a_function():
    while True:
        func = input("Choose function 'CC', 'RV' or 'Q' to quit: ")
        if func == 'Q':
            break
        elif func in MyFunctions:
            MyFunctions[func]()
        else:
            print('Invalid function, Try Again')

pick_a_function()
Reply
#3
Thank you, this is perfect for what I need. Awesome template style also, again thanks mate!
Reply


Forum Jump:

User Panel Messages

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