2017-06-17 3 views
-1

Ich stehe vor dem Problem, wie die Wiederholungseingabe in meinem Hangman Game zu erkennen ist. Bitte hilf mir! Hier ist mein Code: (. Zuerst habe ich die Funktion für das Spiel nennen, und ich benutze while-Schleife) def checkValidGuess():Wie erkenne ich die Wiederholungseingabe in meinem Hangman-Spiel (Python)?

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() 
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"] 
    guess=[] 
    checkValidGuess() 
    if guessword =="0": 
     guessall = input("What is the word: ") 
     checkPlayerWord() 
    else: 
     checkLetterInWords() 
+0

Können Sie weitere Informationen über den Code hinzufügen? – Jeril

+0

@Jeril ok! Ich habe gerade bearbeitet. Vielen Dank! – VincentHoang

Antwort

0

können Sie versuchen, den folgenden 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() 

Die guess = [] muss außerhalb der Schleife deklariert werden, sonst wird für jede Iteration eine neue list

+0

es funktioniert. Ich danke dir sehr ! – VincentHoang

Verwandte Themen