dimanche 18 octobre 2020

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!

Aucun commentaire:

Enregistrer un commentaire