Skip to content Skip to sidebar Skip to footer

How To Replace Dash With Letter - Hangman

I am trying to make a hangman game for class and I am stuck on how to replace a dash (-) with a correctly guessed letter. Like if the word was HAPPY and the user guessed the let

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

def hangman(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 string
    if 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 letter
        for index in occurences:
            hidden_word = hidden_word[:index] + user_input + hidden_word[index + 1:]
        # Return the updated hidden_word
        print(hidden_word)
    # If the user's guess isn't in the string
    else:
        user_input = input("Sorry that letter was not found, please try again: ")

# Find all occurences method
def findOccurrences(s, ch):
    return [i for i, letter in enumerate(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: ")

def findOccurrences(s, ch):
    return [i for i, letter in enumerate(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:

def play_game(secret_word):
guesses_left = 8
while 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.

def hangman(word):
    hidden_word = "-" * len(word)
    print("This is the hidden word " + hidden_word)
    while True:
        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 = 0
        for char in secret_word:
            if char == 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"