Python Help - Hangman game

python

#1

I was making a hangman game and wanted to make it fallout themed. Only problem is whenever it's a word that has more than 1 of the same letter, you can't win (ie. it won't print the "You win" message), even when you get all the letters. What did I do wrong or what do I need to change to fix this bug?

import random

#Make list of words
words = [
    'settler',
    'raider',
    'supermutant',
    'deathclaw',
    'dogmeat',
    'mirelurk',
    'radroach',
    'goul']

while True:
    start = input("Press enter/return to start, or enter Q to quit")
    if start.lower() == 'q':
        break
        
    #pick random word
    secret_word = random.choice(words)
    bad_guesses = []
    good_guesses = []
    
    while len(bad_guesses) < 9 and len(good_guesses) != len(list(secret_word)):
        # #Draw guessed letters and strikes, draw spaces
        for letter in secret_word:
            if letter in good_guesses:
                print(letter, end='')
            else:
                print('_',end='')
                
        print('')
        print('Strikes: {}/7'.format(len(bad_guesses)))
        print('')
        #take a guess
        guess = input("Guess a letter: ").lower()
        
        if len(guess) != 1:
            print("You can only guess a single letter!")
            continue
        elif guess in bad_guesses or guess in good_guesses:
            print("You've already guessed that letter")
            continue   
        elif not guess.isalpha():
            print("You can only guess letters")
            continue
            
        if guess in secret_word:
            good_guesses.append(guess)
            if len(good_guesses) == len(list(secret_word)):
                print("You win! The secret word was {}".format(secret_word))
                break
        else:
            bad_guesses.append(guess)
    else: 
        print("You didn't guess it! My secret word was {}".format(secret_word))
    #Print out win/lose

#2

@ghughes13

There are a bunch of different ways to solve an issue like this.

At this time I would suggest a set.

Example of Set use:

word = 'supermutant'
letters = set(word)
print(letters)
# OUTPUT: {'p', 'r', 'a', 'e', 'u', 'n', 'm', 's', 't'}

Moving on, now how do you use it.

Using the Set

turns = 5

letters_guess = list()

def guess_letter():
    letter_guess.append(raw_input('Enter a letter'))

def check_win():
    """
    Returns True if all letters are correctly guessed
    """
    test = 0
    for letter in letters:
        if letter in letters_guess:
            test += 1
    return test == len(letters)

for i in range(len(word) + turns):
    guess_letter()
    if check_win():
        print("You WIN!!!")
        break
    print("Guess again")

As you can see this is but one way to solve your issue.

If you have any questions feel free to ask.


#3

Thank you Zeziba! .............. <3