Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

I am currently working on the development of a hangman game. In my code, I am having a list variable named "current_word" which is having a bunch of hyphens (-) as the elements of the list. When the user picks a letter accurately it replaces the hyphen with that correctly picked letter in the correct position. My main problem is that let's suppose if I am having two or more same letters in a word then my code is taking the 1st letter but after that, it will say the letter has already been selected therefore it doesn't work and I can't comprehend it.

current_word = "_" * len(word)

a = list(current_word )

print (a)

guessed_word = []

while current_word != word and lives > 0:

    print ("You have %d lives left" % lives)

    guess_letter = input("Please input one letter or type 'exit' to quit.")

    guess_letter = guess_letter.lower()

    if guess_letter == "exit":

        break

    elif guess_letter in guessed_word:

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

    guessed_word.append(guess_letter)

    if guess_letter in word:

        index = theword.find(guess_letter)

        a = list(current_word)

        a[index] = guess_letter

        current_word = "".join(a)

        print ("Correct! \nYour guesses: %s" % (guessed_word))

        print(a)

    else:

        print ("Incorrect, try again")

        lives = lives -1

if current_word == word:

    print ("Well done, you have won!")

1 Answer

0 votes
by (108k points)

Kindly be informed that if you are using find-method() with a string, you can only receive the first occurrence of the letter in the word. In order to get all the occurrences, create a user defined function which will calculate all the occurrences of the element in the word:

def find_all(word, guess):

    return [a for a, letter in enumerate(word) if letter == guess]

You also need to add a "continue" after checking the guessed letter before so that you don't add it to the list repeatedly.

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

Want to become a Python Developer? Check out this insightful Python Certification course.

 

Related questions

0 votes
1 answer
+3 votes
2 answers
0 votes
5 answers

Browse Categories

...