list index out of range in heroku/phython - trying to follow guide to code
<p>I'm trying to follow this guide: <a href="https://medium.com/@sarahnadia/how-to-code-a-simple-twitter-bot-for-complete-beginners-36e37231e67d#.k2cljjf0m" rel="nofollow">https://medium.com/@sarahnadia/how-to-code-a-simple-twitter-bot-for-complete-beginners-36e37231e67d#.k2cljjf0m</a></p>

<p>This is the error message:</p>

<pre><code>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 &lt;module&gt;
    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
</code></pre>

<p>This is the code:</p>

<pre><code>def grab_tweets(api, max_id=None):
    source_tweets=[]
    user_tweets = api.GetUserTimeline(screen_name=user, count=200, max_id=max_id, include_rts=True, trim_user=True, exclude_replies=True)
    max_id = user_tweets[len(user_tweets)-1].id-1
    for tweet in user_tweets:
        tweet.text = filter_tweet(tweet)
        if len(tweet.text) != 0:
            source_tweets.append(tweet.text)
    return source_tweets, max_id

if __name__=="__main__":
     order = ORDER
    if DEBUG==False:
       guess = random.choice(range(ODDS))
     else:
        guess = 0

    if guess == 0:
            if STATIC_TEST==True:
            file = TEST_SOURCE
              print "&gt;&gt;&gt; Generating from {0}".format(file)
            string_list = open(file).readlines()
            for item in string_list:
            source_tweets = item.split(",")    
        else:
            source_tweets = []
            for handle in SOURCE_ACCOUNTS:
                user=handle
                api=connect()
                 max_id=None
                for x in range(17)[1:]:
                    source_tweets_iter, max_id = grab_tweets(api,max_id)
                     source_tweets += source_tweets_iter
</code></pre>

<p>The traceback tells us the error is here:</p>

<pre><code>max_id = user_tweets[len(user_tweets)-1].id-1
</code></pre>

<p>which will be out of range if user_tweets is an empty list.  You can check to make sure it isn't before trying to access its value.</p>

<pre><code>if len(user_tweets) &gt; 0:
  max_id = user_tweets[-1].id-1
</code></pre>

<p>Note the use of <code>user_tweets[-1]</code>.  Using a negative index counts backward from the end, so the last element of a list is always at <code>[-1]</code>.</p>

python heroku