Python Forum

Full Version: Designing Hangman game
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 http://www.pythontutor.com/ 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.
  # https://svnweb.freebsd.org/base/head/share/dict/web2?view=co
  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
    else:
      continue
  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()
      check_guess(grab,creating,letter)
      print(' '.join(creating))
    else:
      print "You Win!"
  else:
    print "Exiting..."
	
start_game()
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.
Output:
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.
Output:
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 "test3.py", line 75, in <module> start_game() File "test3.py", line 68, in start_game check_guess(grab,creating,letter) File "test3.py", 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!!