How do i detect the repeat input in my hangman game (Python)!

前端 未结 1 759
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-28 15:44

I am facing the problem that how to detect the repeat input in my Hangman Game. Please help me! Here is my code:(First i call the function for the game. And i use while loop)

1条回答
  •  礼貌的吻别
    2021-01-28 16:18

    Can you try the following code:

    def checkValidGuess():
    
        if guessword in guess:
            print("Repeat")
    
        elif guessword in num:
            print("You can only input letter a-z")
            print("Try again")
    
        elif len(guessword) > 1:
            print("You can only guess one letter at a time!")
            print("Try again")
    
    
    def checkPlayerWord():
        if guessall == word:
            print("Well done")
        else:
            print("Uh oh!")
    
    
    def checkLetterInWords():
        if guessword.lower() in word:
            print("Well done!", guessword, "is in my word")
        elif guessword.lower() not in word and guessword.lower() not in num:
            print("Try again")
    
    
    choose = input("Enter your choice:")
    readFileWords()
    time = 10
    word = getRandomWord()
    guess = [] # list to store the input values
    while time != 0 and word:
        print("You have", time, "guesses left.")
        guessword = input("Guess a letter or enter '0''to guess the word:")  # This is user input to guess the letter    
        num = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
        checkValidGuess()
        guess.append(guessword) # appending the input to list
        if guessword == "0":
            guessall = input("What is the word: ")
            checkPlayerWord()
        else:
            checkLetterInWords()
    

    The guess = [] must be declared outside the loop, otherwise for every iteration it will create a new list

    0 讨论(0)
提交回复
热议问题