0

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!")
  • Hey Welcome to SO, can you please add the sample input and error you are getting to the question? – Sach Sep 22 '19 at 23:42
  • Sorry about that. I added sample output. The input is the words that are getting printed. – Jeff Bessette Sep 23 '19 at 00:07
  • Possible duplicate of [Python how to get rid of PyDictionary error messages](https://stackoverflow.com/questions/52563826/python-how-to-get-rid-of-pydictionary-error-messages) – Sach Sep 23 '19 at 00:55

0 Answers0