Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Designing Hangman game
#1
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()
Reply
#2
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
Recommended Tutorials:
Reply
#3
Ahh thanks @metulburr I can see the problem more clearly now. Thanks for the quick response!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help in designing a timestamp finder for chapter-less TV shows Daring_T 1 1,850 Oct-26-2020, 09:30 PM
Last Post: Daring_T
  Designing a "game" loop api pitosalas 2 2,472 Jun-07-2020, 03:20 PM
Last Post: pitosalas
  Python Hangman Game - Multiple Letters Problem t0rn 4 4,639 Jun-05-2020, 11:27 AM
Last Post: t0rn
  Hangman metro17 4 3,012 Sep-18-2019, 10:59 AM
Last Post: perfringo
  Trouble coding hangman Tay 1 2,358 Mar-28-2019, 01:57 AM
Last Post: ichabod801
  Hangman code problem KrakowKid 1 2,396 Feb-25-2019, 06:29 PM
Last Post: ichabod801
  Python hangman help A1395 11 7,125 Feb-13-2019, 04:24 PM
Last Post: ichabod801
  Hangman 2skywalkers 3 69,557 Oct-19-2018, 01:49 PM
Last Post: ichabod801
  Hangman Help. 2skywalkers 4 4,185 Jun-26-2018, 02:49 AM
Last Post: ichabod801
  Python Hangman Replacing "_" with letters. 2skywalkers 6 12,069 Jun-25-2018, 12:01 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020