Feb-02-2018, 08:29 PM
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.
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()