I'm a Python-rookie and am currently working on my first real "project" trying to put together a Hangman game, however I'm having an issue with the program when I guess a letter that appears more than once in a word, namely, the program inserts and returns only the first instance of the letter in the word being guessed, as opposed to all duplicates of that letter in the word.

import random

words = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"]
word = random.choice(words)
wordList = [*word]
wordGuessed = False
guess = input("Guess a letter! ")
wordSoFar = ["_"] * len(word)

while wordGuessed == False:
  if guess in wordList:
      for letter in wordList:
          if letter == guess:
            wordSoFar[wordList.index(letter)] = letter
      print(wordSoFar)
      guess = input("Good job! Guess another letter! ")
  else: 
      for letter in wordList: print("_")
      guess = input("Wrong! Try again! ")

Above is the entire code for the game so far. To articulate my problem more concretely, if I guess the letter 't', the console returns

Guess a letter! t
['t', '_', '_', '_', '_']
Good job! Guess another letter!

When it's supposed to be returning

Guess a letter! t
['t', '_', '_', 't', '_']
Good job! Guess another letter!

Furthermore, if I try to guess 't' again after having already done so it gives the exact same result as before, with only the first 't' being show, so if I go through all the letters and guess them all the end result is for example

['t', 'e', 's', '_', '4']

Instead of

['t', 'e', 's', 't', '4']

To be completely honest I can't even diagnose what the issue could be, so I've not been able to try any concrete solution of my own. From my (beginner's) understanding, the following code

      for letter in wordList:
          if letter == guess:
            wordSoFar[wordList.index(letter)] = letter

should insert the correctly guessed letter into all indices of the list where that letter is present in the word being guessed (though obviously that isn't actually the case, since it isn't working)

ADDITION: After looking through some of the existing questions Stack Overflow suggested to me after I finished my write up, I've tried to use code I found in one that goes as follows

          if letter == guess:
            revealed_word += letter
            #wordSoFar[wordList.index(letter)] = letter
          else: revealed_word += "_"

This code does successfully register all instances of a repeating character, however when I use this code as opposed to the original, a new problem appears, that being that now consecutive guesses don't add to the existing output but just infinitely prolong the 'hint' being printed after each guess:

Guess a letter! t
t__t_
Good job! Guess another letter! e
t__t__e___
Good job! Guess another letter! 

All ideas and help greatly appreciated!

1

There are 1 best solutions below

0
On BEST ANSWER

The issue you have origins from this statement wordSoFar[wordList.index(letter)] = letter. After identifying the letter the command wordList.index(letter) searches the wordList again for the first appearance of the letter the user guessed. For this reason it outputs the position of the first appearance. You can mitigate that by storing the position of the letter which is checked in the for loop checking. Your adapted code would look like this

import random

words = ["test1", "test2", "test3", "test4", "test5", "test6", "test7", "test8"]
word = random.choice(words)
wordList = [*word]
wordGuessed = False
guess = input("Guess a letter! ")
wordSoFar = ["_"] * len(word)

while wordGuessed == False:
  if guess in wordList:
      for position, letter in enumerate(wordList):
          if letter == guess:
            wordSoFar[position] = letter
      print(wordSoFar)
      guess = input("Good job! Guess another letter! ")
  else:
      for letter in wordList: print("_")
      guess = input("Wrong! Try again! ")