Bottom Page

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()

Quote
#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
Quote
#3
Ahh thanks @metulburr I can see the problem more clearly now. Thanks for the quick response!!
metulburr likes this post
Quote

Top Page

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

Forum Jump:


Users browsing this thread: 1 Guest(s)