在heroku/phython中列出索引超出范围,尝试遵循cod指南

2024-05-03 14:59:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着按照这个指南:https://medium.com/@sarahnadia/how-to-code-a-simple-twitter-bot-for-complete-beginners-36e37231e67d#.k2cljjf0m

以下是错误消息:

MacBook-Pro-2:heroku_ebooks-master Rupert$ heroku run worker
Running worker on ⬢ desolate-brushlands-56729... up, run.6788
Traceback (most recent call last):
  File "ebooks.py", line 79, in <module>
    source_tweets_iter, max_id = grab_tweets(api,max_id)
  File "ebooks.py", line 51, in grab_tweets
    max_id = user_tweets[len(user_tweets)-1].id-1
IndexError: list index out of range

代码如下:

^{pr2}$

Tags: inpyhttpsidherokuline指南max
1条回答
网友
1楼 · 发布于 2024-05-03 14:59:04

回溯告诉我们错误在这里:

max_id = user_tweets[len(user_tweets)-1].id-1

如果用户的tweets是空列表,则超出范围。在尝试访问它的值之前,可以检查以确保它不是。在

^{pr2}$

注意user_tweets[-1]的用法。使用负索引从末尾向后计数,因此列表的最后一个元素始终位于[-1]。在

相关问题 更多 >