The problem is the list.index method , which returns the index of the first match :
list.index(x[, start[, end]])
Returns an index with a zero index in the list of the first element whose value is x . Raises a ValueError if there is no such element. [...]
If you are also interested in a solution: you can simply iterate over both word and board :
guess = 'a' word = ['a', 'b', 'c'] board = ['_', '_', '_'] board = list(map(lambda x, y: guess if x == guess else y, word, board)) # 3 parameters! print(board) # ['a', '_', '_']
Given that you want to replace, you can also use regular for -loop. This way you avoid creating and dropping lists all the time:
for idx, char in enumerate(word): if char == guess: board[idx] = char
source share