Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display List for User Input
#1
Hello,

I'm trying to display a list of databases to a user and have them make a selection based on number. Once the selection is made, I'd like to construct a connection string from their input. So far, I have basic code to have the user connect but I'm unable to take their database input and construct and append it to the connection string.

import cx_Oracle
import getpass


# Database List
db_list = ['TEST11C', 'TEST12C']
x = 0

print ('\nChoose a database: ')
for i in db_list:
    x += 1
    print(x, i)

while True:
    try:
        # Input username and password
        uname = input('\nEnter username: ')
        pw = getpass.getpass(prompt='Enter password: ')
        conn_str = u'%s/%[email protected]<DATABASE>' % (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.")
Any idea how I can display a list, have the user select a number from the list and take the corresponding database name and add it to the connection string?

Thanks!
Reply
#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/%[email protected]%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
#3
the discussion continued in another thread - https://python-forum.io/Thread-User-Inpu...Dictionary
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem restricting user input in my rock paper scissors game ashergreen 6 547 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Looking for a way to loop until user enters from a list? PythonW19 7 356 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  LinkedList by User Input SantiagoPB 5 711 Mar-03-2021, 09:54 PM
Last Post: deanhystad
  User input/picking from a list AnunnakiKungFu 2 305 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
  Generate Random operator, take user input and validate the user mapypy 4 341 Feb-03-2021, 08:41 PM
Last Post: nilamo
Question Reset list if user regrets Gilush 1 416 Dec-05-2020, 10:55 AM
Last Post: Gilush
  Loop back through loop based on user input, keeping previous changes loop made? hbkpancakes 2 507 Nov-21-2020, 02:35 AM
Last Post: hbkpancakes
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 361 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  user input for multi-dimentional list without a prior iteration using input() Parshaw 6 679 Sep-22-2020, 04:46 PM
Last Post: Parshaw
  How to time a user's input Tbot1000 2 605 Aug-31-2020, 09:39 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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