User Input to Choose from Dictionary - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: User Input to Choose from Dictionary (/thread-9200.html) |
User Input to Choose from Dictionary - anelliaf - Mar-26-2018 Hello, I'm looking to post a list of databases from a dictionary, then from the user input choose which database they want to connect. import pprint, os db_list = { 1: {'name': 'TEST11C'}, 2: {'name': 'TEST12C'}, } print("Databases:") for x, y in db_list.items(): print(x, y) while True: print("Select a database: ") name = int(input()) if name in db_list.keys(): print("You have chosen: " + name) else: print('You chosen wrong!')The user will select a database value based on the key integers of [1,2]. If they select out of that range they'll receive an error. If they select within the range it will take the value of the database. Any help is appreciated. Thanks, Frank RE: User Input to Choose from Dictionary - nilamo - Mar-26-2018 Doesn't what you already have do that? RE: User Input to Choose from Dictionary - anelliaf - Mar-27-2018 No. When I enter a '1' for 'TEST11C' or '2' for the other one, I receive 'You chosen wrong!' when I should I receive the first part of the IF condition. Databases: 1 {'name': 'TEST11C'} 2 {'name': 'TEST12C'} Select a database: 1 You chosen wrong! Select a database: That was after I removed 'int' from input. RE: User Input to Choose from Dictionary - buran - Mar-27-2018 Your code works for me if I correct the TypeError
RE: User Input to Choose from Dictionary - anelliaf - Mar-27-2018 Got it, thx for that! Here's my fix: while True: print("Select a database: ") name = int(input()) if name in db_list.keys(): name = int(name) print("You have chosen: ", name, db_list[name]) else: print('You chosen wrong!')One other thing, how do I display only the database name and not the dictionary entry? Output: Select a database: 1 You have chosen: 1 {'name': 'TEST11C'} Desired output: Select a database: 1 You have chosen: TEST11C RE: User Input to Choose from Dictionary - anelliaf - Mar-27-2018 Finished code: db_list = { 1: {'name': 'TEST11C'}, 2: {'name': 'TEST12C'}, } print("Databases:") for x, y in db_list.items(): print(x, ':', db_list[x]['name']) while True: print("\nSelect a database:") name = int(input()) if x in db_list.keys(): x = int(x) print("\nYou have chosen {0}".format(db_list[x]['name'])) else: print('\nYou chosen wrong!')Thanks for the help! RE: User Input to Choose from Dictionary - buran - Mar-27-2018 A few comments:
I just saw your other thread where you do use list, not dict https://python-forum.io/Thread-Display-List-for-User-Input I will not merge both threads as the other one is much older, but I post this link for completeness. db_list = ['TEST11C', 'TEST12C'] print("Available Databases:") for i, db_name in enumerate(db_list, start=1): print ('{}. {}'.format(i, db_name)) while True: try: selected = int(input('Select a database (1-{}): '.format(i))) db_name = db_list[selected-1] print('You have selected {}'.format(db_name)) break except (ValueError, IndexError): print('This is not a valid selection. Please enter number between 1 and {}!'.format(i)) RE: User Input to Choose from Dictionary - anelliaf - Mar-27-2018 All your points taken into consideration. I'm unsure which direction to go in order to present the user a list of values. I started off with a list but was having trouble presenting the values. With a dictionary, it seems much simpler. I'm a beginner to programming so all the feedback you provided is much appreciated. I updated my code with the following, per your advice: databases = { 1: {'name': 'TEST11C'}, 2: {'name': 'TEST12C'}, } # Print databases listed in dictionary print("Databases:") for x, y in databases.items(): print(x, ':', databases[x]['name']) while True: print("\nSelect a database:") d_val = int(input()) if d_val in databases.keys(): d_val = int(d_val) print("\nYou have chosen {0}".format(databases[d_val]['name'])) else: print('\nYou chosen wrong!') RE: User Input to Choose from Dictionary - buran - Mar-27-2018 I updated my post #7 with an example RE: User Input to Choose from Dictionary - anelliaf - Mar-27-2018 Awesome, got it! Thanks a lot for the example and advice. |