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