2017-06-14 155 views
0

如何检查hang子手游戏的重复输入(字母)?检查重复输入(Python)

例子:

字是苹果

输入猜一个字母:一个

输出做得很好!

然后猜一个字

输入猜一个字母:一个

输出应该是你已经猜到那封信。

我的代码:

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 

感谢。

+0

你有什么这么远吗? –

+0

欢迎来到SO:向我们展示你到目前为止所拥有的。 –

+0

添加了代码@MateenUlhaq – Ned

回答

0

这是一个简单的方法。在循环

guesses = [] 

然后:通过初始化列表中开始

letter = input("Guess a letter or enter '0' to guess the word: ") 

if letter in guesses: 
    print("Already guessed!") 
    continue 

guesses.append(letter) 
+0

弹出此错误: AttributeError:'list'对象没有属性'add' – Ned

+0

对不起,我的意思是'.append' –

+0

它的工作原理,但只适用于不在单词中的字母。例如,单词是苹果,如果您输入'a'并且为第二个输入另一个'a',它仍会检测为完成。但是如果你输入't',而第二个输入是't',它就表示你已经猜到了。它的关闭寿:) 我想我搞砸了正确的字母在某个地方? – Ned

2

您可以将输入存储在列表中,我们将其称为temp

然后,您可以检查当用户输入一个新的字母时,输入是否存在于列表中。

guess = input() 
if guess in temp: 
    print "You've already guessed {}".format(guess) 
else: 
    #do whatever you want 
0

所以你可能要反转在你的程序的检查顺序,使你处理第一再试东西。在更改之后,添加另一个条件来确定该字母是否与已猜出的字母匹配。这导致类似于:

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! 

您需要添加您的其他条件分支,但这应该让你在那里。祝你好运。

+0

它的工作原理,但仅适用于不在单词中的字母。 例如,单词是苹果, 如果您输入'a',并为第二个输入另一'a',它仍然检测为完成。 但是如果你输入't',而第二个输入't'则表示你已经猜到了。 其关闭tho :) @ShawnMehan – Ned

+0

@Ned,我没有完成完整的'while'循环。我只说了一部分......我只是试图告诉你如何处理已经猜到的字母。你需要我为你编写完整的程序吗?我以为你想要一个提示,因为很明显你做这个项目,而不是做的工作...... –

+0

没关系,我解决了它,但现在我坚持把它们分成不同的功能,你想帮忙吗?我呢? – Ned