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 6205829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:20:28+00:00 2026-05-24T05:20:28+00:00

I’m trying to build a markov generator that takes an arbitrary length for the

  • 0

I’m trying to build a markov generator that takes an arbitrary length for the chain of words as a programming exercise, but I’ve found a bug I just can’t quite seem to fix. When I run the markov function, I get list index out of range.

I get the feeling I’m overlooking something obvious, but I’m not sure what. The traceback says the error is on line 41, with words[-1] = nextWords[random.randint(0, len(nextWords)-1)].

Full code is below, sorry if the indenting is messed up.

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.

import random

class Markov(object):
    def __init__(self, open_file):
        self.cache = {}
    self.open_file = open_file
    open_file.seek(0)
    self.wordlist = open_file.read().split()

def get_random_list(self, length):
    i = random.randint(0, len(self.wordlist) - (length - 1))
    result = self.wordlist[i:i + length]
    return result

def find_next_word(self, words):
    candidates = []
    for i in range(len(self.wordlist) - len(words)):
        if self.wordlist[i:i + len(words)] == words and self.wordlist[i+len(words)+1] not in candidates:
            candidates.append(self.wordlist[i+len(words)+1])
    return candidates

def markov(self, length=20, chainlength=2):
    gibberish = []
    words = self.get_random_list(chainlength)
    for i in range(len(words)-1):
        gibberish.append(words[i])
    while len(gibberish) < length:
        #find candidate for next word
        nextWords = self.find_next_word(words)
        gibberish.append(words[-1])
        for i in range(len(words)):
            try:
                words[i] = words[i+1]
            except:
                pass
        words[-1] = nextWords[random.randint(0, len(nextWords)-1)]
    return " ".join(gibberish)
  • 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-05-24T05:20:28+00:00Added an answer on May 24, 2026 at 5:20 am

    If words is empty, then yes that will happen. Trying to access words[-1] in an empty array is just as invalid as words[0]. Add a check to see if len(words) == 0. The same logic holds for nextWords which in this code looks like it too could be empty.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.