I have the following code to check if a word is in the dictionary. If the word does not exist the call to dictionary.meaning returns None. The problem is that it also spits out an error message "Error: The Following Error occured: list index out of range". I did some research and it appeared that I could use a combination of try:, except: but no matter what I tried the error message is still printed out. Here is a test case that shows the problem. How can I make this code work without displaying the index error?
Code:
def is_word(word):
from PyDictionary import PyDictionary
dictionary=PyDictionary()
rtn = (dictionary.meaning(word))
if rtn == None:
return(False)
else:
return (True)
my_list = ["no", "act", "amp", "xibber", "xyz"]
for word in my_list:
result = is_word(word)
if result == True:
print(word, "is in the dictionary")
else:
print(word, "is NOT in the dictionary")
Output:
no is in the dictionary
act is in the dictionary
amp is in the dictionary
Error: The Following Error occured: list index out of range
xibber is NOT in the dictionary
Error: The Following Error occured: list index out of range
xyz is NOT in the dictionary