Edmund
Edmund

Reputation: 1

How to implement error handling on a loop containing dictionary?

How do I implement error-handling in this loop? My loop contains a literal dictionary of words (keys) and its description (values).

wordList = input("Please enter a word to search for")

isWordSearched = False

for word in glossary:
  wordlist == word['term']
    print (word['term'] + " " + "is" + " " + word['description'])
    isWordSearched = True

if isWordSearched == False:
  print("Searched word is not in the glossary")

Upvotes: 0

Views: 49

Answers (1)

Mark SuckSinceBirth
Mark SuckSinceBirth

Reputation: 282

i think you are just missing the if on the wordList == word['term'].

if wordList == word['term']:

But if you really want to add try catch block (coz' you might have KeyError if the key is not in the dict), you can try this:

for word in glossary:
    try:
        if wordList == word['term']:
            print(...)
            isWordSearched = True
    except:
        break

NOTE: next time, please include the code of your question as text to make it easy to debug your code.

Upvotes: 1

Related Questions