Home > OS >  How can I spell a word out for Hangman
How can I spell a word out for Hangman

Time:07-30

I'm having a lot of trouble figuring this out

word = "hello"
hiddenWord = word
            
for i in range (0, len(hiddenWord)):
    hiddenWord = hiddenWord.replace(hiddenWord[i], "_")


userInput = input("Enter letter: ")

for i in range(0, len(word)):
    if word[i] == userInput:
        hiddenWord = hiddenWord.replace(hiddenWord[i], userInput)

print(hiddenWord)

I'm trying to have it find where in the variable "word" the user inputted letter is and replace the underscore in the same location in "hiddenWord" with that letter, so it will gradually spell out the word as the user guesses letters.

What it's doing instead is iterating through "word" and when it reaches a letter equal to one in "word", it will replace every character in "hiddenWord" with that letter. So if I input l, it will iterate twice with no change, as it should, then change the string on the third and output "lllll" when the loop is finished. How would I get it to just change the relevant letters?

CodePudding user response:

If you add print statements to inspect what is happening in the line "hiddenWord = hiddenWord.replace(hiddenWord[i], userInput)", you will see that hiddenWord[i] is an underscore, and "userInput" is the letter you are looking for.

Hence, if you were looking for the letter "e", the line will interpreted as "hiddenWord = hiddenWord.replace("_", "e") which replaces all 5 underscores to "e".

To change the relevant letters, modify the line of code in the "if-statement" to just change a letter at a specific index. The string needs to be converted into a list first, as the "string" object does not support item assignment. Once the for-loop is done, we join the characters in the list together, back into a string.

See the code snippet below.

word = "hello"
hiddenWord = word
            
for i in range (0, len(hiddenWord)):
    hiddenWord = hiddenWord.replace(hiddenWord[i], "_")
    
userInput = input("Enter letter: ")

word_list = list(hiddenWord)

for i in range(0, len(word)):
    if word[i] == userInput:
        word_list[i] = userInput[0]

hiddenWord = ''.join(word_list)
print(hiddenWord)

Hope this helps.

CodePudding user response:

To make it more like the original game, you have some tweaks:

As strings are immutable, you have to convert hiddenWord to a list first (I did a multiply operator trick to create a list with same size of word, filled with _ chars).

Another improvement is that range builtin function, when given only one parameter, assumes the count begins with 0.

Finally, I've added a loop that is done when the word is equal the guess.

word = "hello"
hiddenWord = list(len(word) * '_')
            
while True:    
    userInput = input("Enter letter: ")
    for i in range(len(word)):
        if word[i] == userInput:
            hiddenWord[i] = userInput
    guess = ''.join(hiddenWord)
    print(guess)
    if word == guess:
        print('Congratulations!')
        break
  • Related