如何在我的刽子手游戏(Python)中检测重复输入!
我面临的问题是,如何检测重复输入在我的刽子手游戏。请帮帮我! 这是我的代码:(首先我调用游戏的函数,然后使用while循环) def checkValidGuess():如何在我的刽子手游戏(Python)中检测重复输入!,python,Python,我面临的问题是,如何检测重复输入在我的刽子手游戏。请帮帮我! 这是我的代码:(首先我调用游戏的函数,然后使用while循环) def checkValidGuess(): 您可以尝试以下代码: def checkValidGuess(): if guessword in guess: print("Repeat") elif guessword in num: print("You can only input letter a-z")
您可以尝试以下代码:
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()
必须在循环外声明
guess=[]
,否则每次迭代都会创建一个新的列表
您能添加更多关于代码的信息吗?@Jeril ok!我刚刚编辑过。谢谢
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()