python - 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)
解决方案
显然,syns
不是长度至少为 1 的列表或元组。您是否尝试打印此变量以查看它是否包含值?
推荐阅读
- javascript - 您可以创建一个函数来检查多个输入值并相应地更改它们的样式吗?
- css - 盒子阴影不起作用的主要原因是什么
- c++ - std::vector 结构学生示例
- vue.js - 在 Nuxt.js 中,加载一个简单的谷歌地图
- django - Django - 左连接来自不同数据库的两个查询集
- eloquent - 如果列为空,雄辩使用默认值
- python - 添加列表元素
- node.js - 当堆栈跟踪对于标准输出来说太大时,如何获取 NodeJS 错误?
- c++ - Win32Api 窗口菜单激活问题
- dataframe - 我可以使用 DataFrames 在 Julia 中按天或按月分组吗