1

I'm currently coding a very simple hangman-game in order to get into Python and programming in general. Below you can find what I already have done. At this stage my code is able to pick a word from the list called "word_pool" at random, then display it with blanks instead of letters (so "kitty" turns into "_ _ _ _ _ " for example) and then check if the user's guess is in the word. That already works fine.

What I'm having trouble with is replacing the blanks if a guess is correct. Lets say the word is "kitty" and the user guesses "t", I want the encoded_word to get changed to "_ _ t t _". That's what I tried to do with the for-loop; I let i iterate over each character of the word and if it matches it should replace it in the encoded word. The reason it is i*2-1 in the encoded word is because between the underscores there are spaces. I then try to print the encoded word to see if it worked, but it just prints the underscore with spaces in between. Why is nothing replaced?

import random

word_pool = ["kitty", "dog", "teeth", "sitting"]

print("Guess the word!")
word = word_pool[random.randint(0, len(word_pool)-1)].upper()
print(word)
encoded_word = "_ "*len(word)
print(encoded_word)
guess = input("Which letter do you want to guess? ").upper()

if guess in word:
    print(f"Yes, {guess} is correct!")
    for i in range(0, len(word)):
        if word[i] == guess:
            encoded_word.replace(encoded_word[i*2-1], guess, 1)
            print(encoded_word)
else:
    print(f"No, {guess} isn't correct!")
GyroGoober
  • 61
  • 8
  • you to need reassign the encoded_word.replace() to a variable, replace returns a copy of string it doesnot make changes in the original string. – Rajat Mishra Apr 20 '20 at 12:04

1 Answers1

2

Inspired by Changing one character in a string in Python, I changed your encoded string to a list, because strings in python are immutable. For more details see the above question. Now I can simply modify the letter at their position.

Additionally, I replaced the range with an enumerate, because its nicer and more pythonic. I'm not sure, if you really want to print the result after each replace or after all letters are replaced.

I commented out the random part and the user input for faster execution and testing.

import random

word_pool = ["kitty", "dog", "teeth", "sitting"]

print("Guess the word!")
word = word_pool[0]  # word_pool[random.randint(0, len(word_pool)-1)].upper()
print(word)
encoded_word = ["_"] * len(word)
print(" ".join(encoded_word))
guess = "t"  # input("Which letter do you want to guess? ").upper()

if guess in word:
    print(f"Yes, {guess} is correct!")
    for i, letter in enumerate(word):
        if letter == guess:
            encoded_word[i] = guess

    print(" ".join(encoded_word))
else:
    print(f"No, {guess} isn't correct!")

Output:

Guess the word!
kitty
_ _ _ _ _
Yes, t is correct!
_ _ t t _
Sparky05
  • 4,692
  • 1
  • 10
  • 27
  • if I do this I get: line 16, in encoded_word[i] = guess TypeError: 'str' object does not support item assignment – GyroGoober Apr 20 '20 at 12:16
  • I modified your definition of `encoded_word`, i.e. used `encoded_word = ["_"] * len(word)` instead of `encoded_word = "_ "*len(word)`. – Sparky05 Apr 20 '20 at 12:19
  • Thanks, that worked! I think Ididn't notice it at first. Pretty smart move to have the encoded word just consist of underscores, wo we don't have any issues with the index when replacing the letters – GyroGoober Apr 20 '20 at 13:03