Oct-07-2019, 04:27 AM
I am trying to replace items in a list based on the index. I don't understand why I'm getting the 'index out of range' error.
This is what I have:
This is what I have:
spaces = [] word = "word" guess = "o" spaces.append("'-'," * len(word)) letter_list = list(word) locate = letter_list.index(guess) print(spaces) spaces[locate] = guess print(spaces)This is the error:
Error:["'-','-','-','-',"]
Traceback (most recent call last):
File "tst.py", line 9, in <module>
spaces[locate] = guess
IndexError: list assignment index out of range
This is the desired output:Output:["'-','-','-','-',"]
["'-','o','-','-',"]