Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Designing Hangman game
Hello all,

First time posting ever on a forum for help. I recently got back into coding (some minor courses in high school) and started with Python. I looked online for little projects you can do to test yourself and ended up designing a mini dice rolling program and now working on a hangman game. I really like as it lets me test the code in real-time. When I run my full program (without my dictionary file of course) with all the code there, it works fine with no errors. At random times after initiation it throws an error "IndexError: list assignment index out of range" I investigated the error and know exactly where and why it's occurring but I don't understand why it only happens sometimes. From what I understand, a comment related on this error said, "You cant assign value to a list 'position' if it doesn't exist yet." But the list has already been created so the position does exist. I'm stumped...

Any kind of help and advice is appreciated.

from random import choice

# This function chooses a random word.
def grab_word():
  randword = choice(open("dictionary.txt").readlines()) # Each word is on its own line in the file.
  return randword.lower().strip()

# This function creates the underscores.
def create_US():
  tempword = list(grab_word())
  unders = []
  for i in tempword:
    unders += "_"
  return unders

#This function will repeat the question if input is anything but a single letter.	
def perform_guess():
  guess = raw_input("What is your guess")
  allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  while(len(guess) != 1 or guess not in allowedChars):
    guess = raw_input("What is your guess")
  return guess  
# This function checks the guess and updates the underscore word.
def check_guess(e,f,d):
  for a,b in enumerate(e):
    if b == d:
      f[a] = d # *****Failing right here
  return f
def start_game():
  start = raw_input("Would you like to play Hangman? y or n")
  if start == "y":
    grab = grab_word()
    creating = create_US()
    while "_" in creating:
      letter = perform_guess()
      print(' '.join(creating))
      print "You Win!"
    print "Exiting..."

im not sure why you are getting two different words. grab is one word, but you get your underscores not off that word, but a whole new word with creating in the function create_US. Sometimes then your underscore will be more, or less causing an indexerror i am assuming.

example....i printed out grab after it gets it.
Would you like to play Hangman? y or ny stater What is your guesss s _ _ _ _ _ _ _ _ What is your guesst s t _ t _ _ _ _ _ What is your guesse s t _ t e _ _ _ _ What is your guessr s t _ t e r _ _ _ What is your guessa s t a t e r _ _ _ What is your guess
where this example it chose a whole new word that was less than the expected word. As you can see by guessing the letter "c" it broke it as there is no underscore for that letter.
Would you like to play Hangman? y or ny hypoplanktonic What is your guessh h _ _ _ _ _ _ _ _ _ _ _ _ What is your guessy h y _ _ _ _ _ _ _ _ _ _ _ What is your guessp h y p _ p _ _ _ _ _ _ _ _ What is your guesso h y p o p _ _ _ _ _ o _ _ What is your guessl h y p o p l _ _ _ _ o _ _ What is your guessc Traceback (most recent call last): File "", line 75, in <module> start_game() File "", line 68, in start_game check_guess(grab,creating,letter) File "", line 55, in check_guess f[a] = d # *****Failing right here IndexError: list assignment index out of range
PS you should also be using python3.x
Ahh thanks @metulburr I can see the problem more clearly now. Thanks for the quick response!!
metulburr likes this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trouble coding hangman Tay 1 261 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 255 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 540 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 823 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 900 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 2,137 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk
  Simple Hangman Game Issue andrew95 2 1,416 Apr-02-2018, 02:24 PM
Last Post: andrew95
  TKinter Hangman Game Bumble 1 5,892 Jul-19-2017, 06:56 PM
Last Post: nilamo
  Need help designing a multi-threaded solver 4Dummies 8 2,108 Jun-18-2017, 08:39 PM
Last Post: 4Dummies

Forum Jump:

Users browsing this thread: 1 Guest(s)