Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8371221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:06:44+00:00 2026-06-09T14:06:44+00:00

I am trying to run this code from the beginners’ python book Think Python

  • 0

I am trying to run this code from the beginners’ python book Think Python to do Markov Analysis on a text file. When I run the code provided as a solution, I get an IndexError: List index out of range from the random.py module. What do I need to change?

The code should be the answer to Exercise 8 off of Think Python, but I can’t get it to work.

Traceback:

Traceback (most recent call last):
  File "test.py", line 115, in <module>
    main(*sys.argv)
  File "test.py", line 111, in main
    random_text(n)
  File "test.py", line 76, in random_text
    start = random.choice(suffix_map.keys())
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/random.py", line 274, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty

Edit: I am running it on a .txt file. It’s the text of a book downloaded from Project Gutenburg.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:06:46+00:00Added an answer on June 9, 2026 at 2:06 pm

    The file you are running the program on doesn’t have a Gutenberg header. It needs to have a line that starts with:

    *END*THE SMALL PRINT!
    

    Only text after that will be read (this occurs in the skip_gutenberg_header function). As an example, running the code on the file

    Header info
    *END*THE SMALL PRINT!
    
    He was very clever, be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself.
    

    produces (as one example):

    a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. cannot make speeches, Emma:" he soon cut it all himself. be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Under Python 3.1, when trying to run this code: from urllib import request def
I got this code from Andy Tanenbaum's book. I am trying to run it.
I am trying to run this code: ItemTaxonomy iTaxonomy = from itemTaxonomy in connection.ItemTaxonomy
I'm trying to run this script that basically reads/interprets text in from a textfile
I am trying to run demo code from Matplotlib: the legend_picking example . This
I'm trying to run this code taken from Sun Java site (I didn't copy
I am trying to run this piece of code taken from http://coffeescriptcookbook.com embedding it
I'm trying to run this code but this error appear: Uncaught TypeError: string is
I'm trying to run this code: let coins = [50, 25, 10, 5, 2,1]
I am trying to run this code : exec myStoredProcedure Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier), 744,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.