0

Is there a way to input a multiple word string to search multiple matching dictionary keys? - Thank you.

def concept(word):
# use this to list python file titles and links to open them in a new tab
files    = {1:"http://file_title0001.py",
            2:"file_title0002.txt",
            3:"file_title0003.txt",
            4:"file_title0004.txt",
            5:"file_title0005.txt",
            6:"file_title0006.txt",
            7:"file_title0007.txt",    
            8:"file_title0008.txt",
            9:"file_title0009.txt"}
# change keys to searchable simple keyword phrases. 
concepts = {'GAMES':[1,2,4,3,3],
            'BLACKJACK':[5,3,5,3,5],
            'MACHINE':[4,9,9,9,4],
            'DATABASE':[5,3,3,3,5],
            'LEARNING':[4,9,4,9,4]}
if word.upper() not in concepts:
    print("Nothing Found in Database")
    return
for pattern in concepts[word.upper()]:
    print(files[pattern])

while True:
    concept(input("Enter Concept Idea: "))
    print("\n")
  • See [this](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) and [this](https://stackoverflow.com/questions/3295938/else-clause-on-python-while-statement). – Selcuk Dec 04 '18 at 23:00

2 Answers2

2

I think this is what you want:

def print_big(word):
    # use this to list python file titles and links to open them in a new tab
    files = {1:"http://file_title0001.py",
            2:"file_title0002.txt",
            3:"file_title0003.txt",
            4:"file_title0004.txt",
            5:"file_title0005.txt",
            6:"file_title0006.txt",
            7:"file_title0007.txt",    
            8:"file_title0008.txt",
            9:"file_title0009.txt"}
    # change keys to searchable simple keyword phrases. 
    concepts = {'GAMES':[1,2,4,3,3],
                'BLACKJACK':[5,3,5,3,5],
                'MACHINE':[4,9,9,9,4],
                'DATABASE':[5,3,3,3,5],
                'LEARNING':[4,9,4,9,4]}
    if word.upper() not in concepts:
        print("Nothing Found in Database")
        return
    for pattern in concepts[word.upper()]:
        print(files[pattern])

print_big(input("Enter Concept Idea: "))
Pinyi Wang
  • 823
  • 5
  • 14
0

You can use a try/except clause to cover the case in which the key is not present (KeyError):

try:
    for pattern in concepts[word.upper()]:
        print(files[pattern])
except KeyError as err:
    print("Nothing Found in Database for '{}'".format(err))
a_guest
  • 34,165
  • 12
  • 64
  • 118