Python 我得到了一个明确的错误,即使一切似乎都很好
我得到了这个错误:Python 我得到了一个明确的错误,即使一切似乎都很好,python,nameerror,Python,Nameerror,我得到了这个错误: Traceback (most recent call last): File "C:\Python27\botid.py", line 23, in <module> fiList = {msg:submission.ups + len(coList)} NameError: name 'coList' is not defined 我觉得很好。所有的搜索结果最终都是拼写错误,但我的结果似乎不错(我希望)只有在注释非空的情况下才会定义coList。
Traceback (most recent call last):
File "C:\Python27\botid.py", line 23, in <module>
fiList = {msg:submission.ups + len(coList)}
NameError: name 'coList' is not defined
我觉得很好。所有的搜索结果最终都是拼写错误,但我的结果似乎不错(我希望)只有在注释非空的情况下才会定义coList。如果注释为空,则永远不会定义coList,因此产生名称错误
看起来您在循环的每个迭代中都在重新定义coList,但看起来您实际上希望附加到它?我认为您应该尝试:
coList = []
for comment in comments:
coList.append(comment.author.name)
您尝试的是:
for comment in comments:
coList = [comment.author.name]
对于每个注释,这个循环都会将colList重置为当前注释作者姓名的单个项目列表,但我可以从您的注释中看出您已经理解了这一点
其他对列表理解的评论在imo中要好得多,我个人也会使用:
colist = [comment.author.name for comment in comments]
这一行看起来更清晰,你可以清楚地看到它的意图是什么,评论中的作者列表。我认为最简单的解决方案是列表理解:
coList = [comment.author.name for comment in comments]
这样,如果注释为空,则会得到一个空列表,否则会显示作者姓名。此外,考虑到您输入的内容,最好将其称为
作者列表
啊,不,它应该只检查一次,但这没什么大不了的。我尝试在for循环之前定义它,但仍然得到相同的错误。
coList = [comment.author.name for comment in comments]