I'm trying to make a program that will grab a random word from a JSON file and print it and it's definition using PyDictionary. It works occasionally but I think the issue I am having is displaying output from dictionary.meaning(word) when the word has multiple meanings. I get an IndexError when that appears the be the case.
example outputs: expected: tinamidae Noun ['comprising the tinamous']
unwanted result: unmaterially Error: The Following Error occured: list index out of range No definition found!
import json
import random
from PyDictionary import PyDictionary
dictionary = PyDictionary()
with open('C:\\Users\\jabes\\Desktop\\words_dictionary.json') as json_file:
words = json.load(json_file)
word = random.choice(list(words.keys()))
print(word)
try:
meanings = dictionary.meaning(word)
if meanings:
for k,v in meanings.items():
print(k, v)
else:
print("No definition found!")
except Exception as error:
print(error)
print("Exiting!")