pythonif-statementwhile-loopcounter

Python letter guess game


I want to create the hangman python game, so I created a simpler piece of the game. The idea of the game is to guess the letters in a randomly generated word. Here is what I have so far. I listed the problem and questions I have at the bottom.

import random


words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
random_word = random.choice(words)
count = 0


while True:                                     #repeatedly asks you to guess the letter in a random word
    guess = str(input("Guess a letter:  "))
    guess = guess.lower()


    if guess in random_word:             #checks if the letter you input is in the random generated word
        print("Yay, its in the word")
      
    
    else:                          
        count += 1
        print("Not in the word, attempts: %d" % count)
        
        if count > 5:                   
            print("You have reached max attempts")
            print("Sorry, but hangman died! You lose")
            break
        else:
            continue

The problem I have: When the user guesses a letter, they can guess it again infinite times. How can I make it so that the user can't repeatedly guess the same letter?

Is there a way to make sure the user doesn't guess the same letter? This could be a problem in an actual hangman game when there are several letters that are the same. Any help/feedback appreciated, thanks!


Solution

  • Here is one way of doing it.

    import random
    
    
    words = ['holiday', 'american', 'restaurant', 'computer', 'highschool', 'programming']
    random_word = random.choice(words)
    count = 0
    guess_list = []
    
    while True:                                     #repeatedly asks you to guess the letter in a random word
        guess = str(input("Guess a letter:  "))
        guess = guess.lower()
    
        if guess not in guess_list:
            if guess in random_word:             #checks if the letter you input is in the random generated word
                print("Yay, its in the word")
    
            else:                          
                count += 1
                print("Not in the word, attempts: %d" % count)
    
                if count > 5:                   
                    print("You have reached max attempts")
                    print("Sorry, but hangman died! You lose")
                    break
                    
        else:
            print("You have already guessed {}. Try again".format(guess))
            print(set(guess_list))
            
        guess_list.append(guess)
    

    Sample output (word is computer):

    Guess a letter:  c
    Yay, its in the word
    Guess a letter:  e
    Yay, its in the word
    Guess a letter:  e
    You have already guessed e. Try again
    {'e', 'c'}
    Guess a letter:  e
    You have already guessed e. Try again
    {'e', 'c'}
    Guess a letter:  w
    Not in the word, attempts: 1
    Guess a letter:  m
    Yay, its in the word
    Guess a letter:  m
    You have already guessed m. Try again
    {'w', 'm', 'e', 'c'}
    Guess a letter:  
    

    Notes:

    1. A guess_list is created that keeps record of all the guesses.
    2. After each guess the letter is appended to the list.
    3. When the user repeats the guess they are warned. We use a set so only the unique elements in the guess list are displayed.

    The code can be refined further, but must do the job.