本文介绍了检查输入是否重复(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检查check子手游戏的重复输入(字母)?
How do i check for a repeating input(letter) for a hangman game?
示例:
单词是苹果
输入是猜字母:a
输出完成!
然后猜测下一个单词
输入是猜字母:a
输出应该是您已经猜到了那封信.
output should be you already guess that letter.
我的代码:
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
谢谢.
推荐答案
这是一种简单的方法.首先初始化列表:
Here's an easy way. Start by initializing a list:
guesses = []
然后在循环中
letter = input("Guess a letter or enter '0' to guess the word: ")
if letter in guesses:
print("Already guessed!")
continue
guesses.append(letter)
这篇关于检查输入是否重复(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!