How To Replace Dash With Letter - Hangman
Solution 1:
I created a very simple example for this. Essentially if user_input in word
then we find the index of the user input and replace it with the hidden letter. The code is not very well written :) but it does the job
defhangman(word):
# Replace word with dashes
hidden_word = "-" * len(word)
print("This is the hidden word " + hidden_word)
# Get user's guess
user_input = input("Guess a letter: ")
# If the user's guess exists in the stringif user_input in word:
# Find all occurences of user's guess in word
occurences = findOccurrences(word, user_input)
# For each occurenc, replace that dash in the string with the correct letterfor index in occurences:
hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
# Return the updated hidden_wordprint(hidden_word)
# If the user's guess isn't in the stringelse:
user_input = input("Sorry that letter was not found, please try again: ")
# Find all occurences methoddeffindOccurrences(s, ch):
return [i for i, letter inenumerate(s) if letter == ch]
hangman("hello")nput("Guess a letter: ")
if user_input in word:
occurences = findOccurrences(word, user_input)
for index in occurences:
hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
print(hidden_word)
else:
user_input = input("Sorry that letter was not found, please try again: ")
deffindOccurrences(s, ch):
return [i for i, letter inenumerate(s) if letter == ch]
hangman("hello")
Solution 2:
Going through the string is not worth programming. Just set the hangman_dash
variable to ''
every iteration of the loop and make a for loop like the one below to check if each of the letters are correct:
defplay_game(secret_word):
guesses_left = 8while guesses_left > 0:
hangman_dash=''for i in secret_word:
if i in good_guesses:
hangman_dash+=i
else:
hangman_dash+='-'print("The word now looks like this: " + (hangman_dash))
...
So if a letter in secret_word
has been guessed (or is in the good_guesses
string) then whatever that letter is will be added to hangman_dash
. Otherwise, '-'
will be added.
Solution 3:
I've expanded upon the code that redline provided. It checks for alpha characters as well.
defhangman(word):
hidden_word = "-" * len(word)
print("This is the hidden word " + hidden_word)
whileTrue:
user_input = input("Guess a letter: ")
if user_input.isalpha():
if user_input in word:
index = word.find(user_input)
hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
print(hidden_word)
else:
print("Sorry that letter was not found, please try again.")
else:
print("Please use letters in the alphabet.")
hangman("word")
As requested, I've added an example of words that contain two of the same letters. I hope this helps.
def hangman(secret_word):
hangman_dash = len(secret_word) * "-"while True:
user_input = input("Guess:")
index = 0forcharin secret_word:
ifchar == user_input:
hangman_dash = hangman_dash[:index] + char + hangman_dash[index + 1:]
print(hangman_dash)
index += 1
hangman("happy")
Post a Comment for "How To Replace Dash With Letter - Hangman"