1

I am making a hangman game but have run into a problem when there is more than one letter in the word, as it only shows the letter once when I guess it.

I need to check if a letter is in a list more than once, and find the indices of both the letters so I can show them in another list (guess_list)

My code:

import random
from words import words
from test import one, two, three, four, five, six, seven, eight, nine

lives_left = 9
letters_picked = []

# choose word
word_origional = words[random.randint(0, len(words))]

#find length of word and create blank list
guess_list = ["_"]*len(word_origional)

#turn word's letters into list of letters
word = list(word_origional)

#user guesses letters
while lives_left > 0 and guess_list != word:
    print("Word: " + str(guess_list))
    print("Wrong letters: " + str(letters_picked))
    guess = input("Guess: ")
    if guess in word:
        print("Letter in word!")
        mult = word.count(guess)
        index = word.index(guess)
        guess_list[index] = guess
    elif guess not in word:
        print("Letter not in word!")
        letters_picked.append(guess)
        lives_left -= 1
    
    #calculate which hangman to show
    if lives_left == 9:
        hangman = ("Full lives!")
    elif lives_left == 8:
        hangman = one
    elif lives_left == 7:
        hangman = two
    elif lives_left == 6:
        hangman = three
    elif lives_left == 5:
        hangman = four
    elif lives_left == 4:
        hangman = five
    elif lives_left == 3:
        hangman = six
    elif lives_left == 2:
        hangman = seven
    elif lives_left == 1:
        hangman = eight
    
    #show hangman and lives left
    print(hangman)
    print(str(lives_left) + " lives left")
    

#show fail message
if lives_left == 0:
    print(nine)
    print("You ran out of lives!")
    print("The word was " + word_origional)
#show win message
else:
    print("Congrats! You got the word!")
    print("You had " + lives_left + "lives left.")
  • Does this answer your question? [How to find all occurrences of an element in a list](https://stackoverflow.com/questions/6294179/how-to-find-all-occurrences-of-an-element-in-a-list) – mrCopiCat Sep 02 '22 at 11:10

0 Answers0