2017-06-14 154 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