0

I'm trying to create a hangman application but while replacing the hidden characters, I'm getting all characters replaced instead of the ones found:

import sys
import time
import logging
import msvcrt
import requests
import random


def get_word_list():
    response = requests.get(
        'https://www.mit.edu/~ecprice/wordlist.10000',
        timeout=10
    )

    sort_into_dict = response.content.decode('utf-8')
    wordlist = sort_into_dict.splitlines()

    return wordlist


words = get_word_list()

word = random.choice(words)


def hangman():
    print('To Hang the man think of a ' + str(len(word)) + ' letter word')
    guess = "*" * int(len(word))
    print('The word to guess is ' + guess)
    print("Wordy word, when will the hanging commence? Type a letter and find out")
    while str(guess) != str(word):
        print(word)
        print("Please pick a letter")
        letter = msvcrt.getwch()
        # While works well if entire word is inputted, starting for and if 
#       for x in letter:
        if letter in word:
            print('BeforeReplace ' + guess)
            rw = (word.rfind(letter))
            guess = guess.replace(guess[rw], letter)
            print('AfterReplace ' + guess)
            print('Nice this letter exists at position ' + str(1 + (word.rfind(letter))))
        else:
            print("It isn't")
        continue

    print("The Man is hanged! - word is " + word)


hangman()

time.sleep(3)

guess is originally the len of the picked word

Expecting the above to replace the found letter location in guess with letter picked

if letter in word:
    print('BeforeReplace ' + guess)
    rw = (word.rfind(letter))
    guess = guess.replace(guess[rw], letter)
    print('AfterReplace ' + guess)
    print('Nice this letter exists at position ' + str(1 + (word.rfind(letter))))

OUTPUT:

To Hang the man think of a 7 letter word
The word to guess is *******
Wordy word, when will the hanging commence? Type a letter and find out
leaving
Please pick a letter
BeforeReplace *******
AfterReplace lllllll
Nice this letter exists at position 1
leaving
Please pick a letter
BeforeReplace lllllll
AfterReplace ggggggg
Nice this letter exists at position 7
leaving
Please pick a letter
It isn't
leaving
Please pick a letter
It isn't
leaving
Please pick a letter
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Eyalp
  • 1
  • 1
  • Well, `str.replace()` replaces **all** occurrences of the character in the string. Since all your characters are `"*"`, for any given value of `rw`, `guess[rw]` will always be `"*"` and so all `*` characters are replaced. – Martijn Pieters Nov 27 '22 at 15:15
  • See [Changing one character in a string](https://stackoverflow.com/q/1228299) for techniques that *do* work. – Martijn Pieters Nov 27 '22 at 15:17
  • Thank you for the explanation, it was very helpful - I tried using the same on a different string and it seemed to work, I didn't take into account it picks the char in that position and then changes every char based on that. Much appreciate, will look at what you posted. – Eyalp Nov 27 '22 at 15:23

0 Answers0