首页 > 解决方案 > An error happens in my wordnet python code: IndexError; List index out of range

问题描述

I am working on some small code, and the error

IndexError: list index out of range

always comes after execution of the last line of code, any help?

from nltk.corpus import wordnet  # this is where you load the wordnet corpus

user_input = input("What should I reverse, user?")  # this is where you get the items for the string

str = user_input  # this is the initial string

stringlength = len(str)  # this is where you calculate length of the list

slicedString = str[stringlength::-1]  # this is where you start slicing

syns = wordnet.synsets(slicedString)  # this gives the reversed word to the dictionary

print(syns[0].definition())  # this prints the meaning (if there is one)

I have looked in books for the answer, but nothing is working. (PyCharm Edu 4.0)

标签: pythonnltkwordnet

解决方案


显然,syns不是长度至少为 1 的列表或元组。您是否尝试打印此变量以查看它是否包含值?


推荐阅读