user3160631
user3160631

Reputation: 11

Python - Hangman Game with multiple letters in one word

I am new to python and have a problem with a hangman game that I was making. So I have a list called "current" which is filled with hyphens (-) the same amount of times as the length of the word the person is guessing. When they guess a letter correctly it replaces the hyphen with the letter in the correct place. The problem I have is that if there are 2 or more of the same letter in a word then it works for the first time the letter comes up but after that it will say the letter has already been guessed therefore it doesn't work and I cant figure out how to fix this.

current = "_" * len(theword)
x = list(current)
print (x)

guessed = []

while current != theword and lives > 0:

    print ("You have %d lives left" % lives)
    guess = input("Please input one letter or type 'exit' to quit.")
    guess = guess.lower()


    if guess == "exit":
        break
    elif guess in guessed:

        print ("You have already guessed this letter, please try again.")



    guessed.append(guess)

    if guess in theword:
        index = theword.find(guess)
        x = list(current)
        x[index] = guess
        current = "".join(x)
        print ("Correct! \nYour guesses: %s" % (guessed))
        print(x)


    else:
        print ("Incorrect, try again")
        lives = lives -1


if current == theword:
    print ("Well done, you have won!")

Thanks

Upvotes: 1

Views: 3858

Answers (2)

Assios
Assios

Reputation: 126

By using the string.find-method, you only get the first occurrence of the character in the word. By defining this method, you get all occurrences:

def find_all(word, guess):
    return [i for i, letter in enumerate(word) if letter == guess]

You should also add a "continue" after checking if you've already guessed the letter before, so that you don't add it to the list again.

This should work:

def find_all(word, guess):
    return [i for i, letter in enumerate(word) if letter == guess]

current = "_" * len(theword)
x = list(current)
print (x)

guessed = []

while current != theword and lives > 0:

    print ("You have %d lives left" % lives)
    guess = input("Please input one letter or type 'exit' to quit.")
    guess = guess.lower()


    if guess == "exit":
        break
    elif guess in guessed:
        print ("You have already guessed this letter, please try again.")
        continue

    guessed.append(guess)

    if guess in theword:
        indices = find_all(theword, guess)
        x = list(current)
        for i in indices:
            x[i] = guess
            current = "".join(x)
            print ("Correct! \nYour guesses: %s" % (guessed))
            print(x)

    else:
        print ("Incorrect, try again")
        lives = lives -1

Upvotes: 1

sshashank124
sshashank124

Reputation: 32189

Instead of:

if guess in theword:
        index = theword.find(guess)
        x = list(current)
        x[index] = guess
        current = "".join(x)
        print ("Correct! \nYour guesses: %s" % (guessed))
        print(x)

Try this:

for index,character in enumerate(theword):
    x = list(current)
    if character == guess:
        x[index] = guess

Upvotes: 0

Related Questions