检查输入中的重复(Python)
如何检查刽子手游戏的重复输入(字母) 例如: 单词是苹果 输入是猜测一个字母:a 输出做得很好 然后猜猜下一个单词 输入是猜测一个字母:a 输出应该是您已经猜到的字母 我的代码:检查输入中的重复(Python),python,python-3.x,input,Python,Python 3.x,Input,如何检查刽子手游戏的重复输入(字母) 例如: 单词是苹果 输入是猜测一个字母:a 输出做得很好 然后猜猜下一个单词 输入是猜测一个字母:a 输出应该是您已经猜到的字母 我的代码: def checkValidGuess(): word = getHiddenWord() lives = 10 num = ["1","2","3","4","5","6","7","8","9",] #guessed = '' while lives != 0 and word: print("\nYou
def checkValidGuess():
word = getHiddenWord()
lives = 10
num = ["1","2","3","4","5","6","7","8","9",]
#guessed = ''
while lives != 0 and word:
print("\nYou have", lives,"guesses left")
letter = input("Guess a letter or enter '0' to guess the word: ")
if letter.lower() in word:
print("Well done!", letter, "is in my word!")
lives -= 1
elif len(letter)>1:
print("You can only guess one letter at a time!")
print("Try again!")
elif letter in num:
print("You can only input letter a - z!")
print("Try again!")
#elif letter in guessed:
#print("repeat")
elif letter == "0":
wword = input("What is the word?").lower()
if wword == word:
print("Well done! You got the word correct!")
break
else:
print("Uh oh! That is not the word!")
lives -= 1
#elif letter == "":
#print("Uh oh! the letter you entered is not in my word.")
#print("Try again!")
else:
print("Uh oh! the letter you entered is not in my word.")
print("Try again!")
lives -= 1
谢谢。您可以将输入存储在列表中,我们称之为
temp
然后,当用户输入新字母时,可以检查列表中是否存在输入
guess = input()
if guess in temp:
print "You've already guessed {}".format(guess)
else:
#do whatever you want
您可以将输入存储在一个列表中,我们称之为
temp
然后,当用户输入新字母时,可以检查列表中是否存在输入
guess = input()
if guess in temp:
print "You've already guessed {}".format(guess)
else:
#do whatever you want
因此,您可能希望颠倒程序中检查的顺序,以便先处理问题,然后再试一次。在该更改之后,添加另一个条件,确定字母是否与已经猜测的字母匹配。这会导致如下结果:
already_guessed = set() # I made this a set to only keep unique values
while lives > 0 and word: # I changed this to test > 0
print(f"\nYou have {lives} guesses left") # I also added an f-string for print formatting
letter = input("Guess a letter or enter '0' to guess the word: ")
if len(letter) > 1:
print("You can only guess one letter at a time!")
print("Try again!")
continue # If you reach this point, start the loop again!
elif letter in already_guessed:
print("You already guessed that!")
print("Try again")
continue
elif letter in num:
print("You can only input letter a - z!")
print("Try again!")
continue
elif letter.lower() in word:
print("Well done!", letter, "is in my word!")
lives -= 1
else:
already_guessed.update(letter)
# It wasn't a bad character, etc. and it wasn't in the word\
# so add the good character not in the word to your already guessed
# and go again!
您需要添加其他条件分支,但这应该可以实现。祝你好运。因此,你可能想在你的程序中颠倒支票的顺序,这样你就可以处理问题,然后再试一次。在该更改之后,添加另一个条件,确定字母是否与已经猜测的字母匹配。这会导致如下结果:
already_guessed = set() # I made this a set to only keep unique values
while lives > 0 and word: # I changed this to test > 0
print(f"\nYou have {lives} guesses left") # I also added an f-string for print formatting
letter = input("Guess a letter or enter '0' to guess the word: ")
if len(letter) > 1:
print("You can only guess one letter at a time!")
print("Try again!")
continue # If you reach this point, start the loop again!
elif letter in already_guessed:
print("You already guessed that!")
print("Try again")
continue
elif letter in num:
print("You can only input letter a - z!")
print("Try again!")
continue
elif letter.lower() in word:
print("Well done!", letter, "is in my word!")
lives -= 1
else:
already_guessed.update(letter)
# It wasn't a bad character, etc. and it wasn't in the word\
# so add the good character not in the word to your already guessed
# and go again!
您需要添加其他条件分支,但这应该可以实现。祝你好运。这里有一个简单的方法。从初始化列表开始:
guesses = []
然后在循环中:
letter = input("Guess a letter or enter '0' to guess the word: ")
if letter in guesses:
print("Already guessed!")
continue
guesses.append(letter)
这里有一个简单的方法。从初始化列表开始:
guesses = []
然后在循环中:
letter = input("Guess a letter or enter '0' to guess the word: ")
if letter in guesses:
print("Already guessed!")
continue
guesses.append(letter)
到目前为止你有什么?欢迎来到so:向我们展示你到目前为止有什么。添加了代码@MateenUlhaqCodes Added@shawneman你到目前为止有什么?欢迎来到so:向我们展示你到目前为止有什么。添加了代码@MateenUlhaqCodes Added@shawneman此错误弹出:AttributeError:'list'对象没有属性'add'抱歉,我的意思是
.append
它只适用于单词中的字母,而不适用于单词中的字母。例如,单词是apple,如果您输入'a',第二次输入另一个'a',它仍然检测到做得很好。但如果你输入“t”,第二次输入另一个“t”,它表示你已经猜到了。我想我把单词中正确的字母搞砸了?把这个放在最上面,放在所有其他的if
s之前。如果订购if
s.Holyy,请小心!!谢谢你,成功了:D非常感谢!!!感谢帮助:d此错误弹出:AttributeError:'list'对象没有属性'add',抱歉,我的意思是.append
它只适用于单词中没有的字母。例如,单词是apple,如果您输入'a',第二次输入另一个'a',它仍然检测到做得很好。但如果你输入“t”,第二次输入另一个“t”,它表示你已经猜到了。我想我把单词中正确的字母搞砸了?把这个放在最上面,放在所有其他的if
s之前。如果订购if
s.Holyy,请小心!!谢谢你,成功了:D非常感谢!!!感谢您的帮助:DIt只适用于单词中的字母,而不适用于单词中的字母。例如,单词是apple,如果您输入'a',第二次输入另一个'a',它仍然检测到做得很好。但如果你输入“t”,第二次输入另一个“t”,它表示你已经猜到了。它的结尾是:)@ShawnMehan@Ned,我没有完成完整的循环。我只提到了其中的一部分……我只是想告诉你如何处理已经猜到的信件。你需要我为你写完整的程序吗?我以为你想要一个提示,因为很明显你是作为一个项目来做的,而不是完成的工作…没关系,我解决了它,但现在我一直在把它们分成不同的函数,你愿意帮我吗?它只对字母有效,而不是单词中的字母。例如,单词是apple,如果您输入'a',第二次输入另一个'a',它仍然检测到做得很好。但如果你输入“t”,第二次输入另一个“t”,它表示你已经猜到了。它的结尾是:)@ShawnMehan@Ned,我没有完成完整的循环。我只提到了其中的一部分……我只是想告诉你如何处理已经猜到的信件。你需要我为你写完整的程序吗?我以为你想要一个提示,因为很明显你是作为一个项目来做的,而不是完成的工作…没关系,我解决了它,但现在我坚持把它们分成不同的功能,你愿意帮我吗?