Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display List for User Input
#2
I was able to find an interactive menu program and adjust it slightly to do what I need but I'm still having an issue with the database menu:

# Import the modules needed to run the script.
import sys, os
import cx_Oracle
import getpass

# Main definition - constants
menu_actions = {}

# =======================
#     MENUS FUNCTIONS
# =======================
# Main menu
def main_menu():
    os.system('cls')

    print("Welcome!\n")
    print("Please choose from the menu below:")
    print("1. Change Password")
    print("2. Unlock Account")
    print("\n0. Quit")
    choice = input(" >>  ")
    exec_menu(choice)

    return

# Execute menu
def exec_menu(choice):
    os.system('cls')
    ch = choice.lower()
    if ch == '':
        menu_actions['main_menu']()
    else:
        try:
            menu_actions[ch]()
        except KeyError:
            print("Invalid selection, please try again.\n")
            menu_actions['main_menu']()
    return

def db_menu(choice):
    os.system('cls')
    ch = choice.lower()
    print("Choose a database:\n")
    if ch == '':
        menu_actions['main_menu']()
    else:
        try:
            db_list[ch]()
        except KeyError:
            print("Invalid selection, please try again.\n")
            menu_actions['main_menu']()


# Database Login Menu
def db_login():
    while True:
        try:
            # Input username and password
            uname = input('\nEnter username: ')
            pw = getpass.getpass(prompt='Enter password: ')
            conn_str = u'%s/%s@%db' % (uname, pw)
            conn = cx_Oracle.connect(conn_str)
            c = conn.cursor()
            break
        except cx_Oracle.DatabaseError as e:
            error, = e.args
            print("\nInvalid username/password.  Please try again.")

# Menu 1
def menu1():
    print("Menu Item: Change Password\n")
    db_menu()
    db_login()
    print("9. Back")
    print("0. Quit")
    choice = input(" >>  ")
    exec_menu(choice)
    return

# Menu 2
def menu2():
    print("Menu Item: Unlock Account\n")
    db_menu()
    db_login()
    print("9. Back")
    print("0. Quit")
    choice = input(" >>  ")
    exec_menu(choice)
    return

# Back to main menu
def back():
    menu_actions['main_menu']()

# Exit program
def exit():
    sys.exit()


# =======================
#    MENUS DEFINITIONS
# =======================

# Database list
db_list = {
    '1': test11c,
    '2': test12c,
}
#db_list = ['TEST11C', 'TEST12C']

# Menu definition
menu_actions = {
    'main_menu': main_menu,
    '1': menu1,
    '2': menu2,
    '9': back,
    '0': exit,
}

# =======================
#      MAIN PROGRAM
# =======================

# Main Program
if __name__ == "__main__":
    # Launch main menu
    main_menu()
Reply


Messages In This Thread
Display List for User Input - by anelliaf - Mar-21-2018, 02:00 PM
RE: Display List for User Input - by anelliaf - Mar-22-2018, 03:26 PM
RE: Display List for User Input - by buran - Mar-27-2018, 01:51 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,097 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,095 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  How to display <IPython.core.display.HTML object>? pythopen 3 46,071 May-06-2023, 08:14 AM
Last Post: pramod08728
  restrict user input to numerical values MCL169 2 944 Apr-08-2023, 05:40 PM
Last Post: MCL169
  user input values into list of lists tauros73 3 1,091 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,129 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,129 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 1,961 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  functional LEDs in an array or list? // RPi user Doczu 5 1,659 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  How to split the input taken from user into a single character? mHosseinDS86 3 1,206 Aug-17-2022, 12:43 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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