2017-06-14 14 views
0

ハングマンゲームの繰り返し入力(文字)を確認するにはどうすればよいですか?入力の繰り返しを確認する(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

へようこそ:あなたがこれまで持っているものを私たちに教えてください。 –

+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:「リスト」オブジェクトが属性を持っていない – Ned

+0

「追加」申し訳ありませんが、私は '.append' –

+0

を意味それは唯一の単語内の文字はないため動作します。たとえば、単語はリンゴ、 'a'を入力し、もう1つの入力に 'a'を入力した場合でも、それはまだ完了したと検出されます。しかし、あなたが 't'を入力し、2番目の入力に別の 't'を入力すると、あなたはすでに推測しています。その近くのtho :) 私は単語の正しい手紙のためにどこかを台無しにしたと思いますか? – 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

コードが追加されましたが、単語に含まれていない文字のみが機能します。 例:wordはapple、 です。 'a'を入力し、2番目の入力にもう1つ 'a'を入力すると、それでもまだ完了したことが検出されます。 't'を入力し、2番目の入力に別の 't'を入力すると、あなたはすでに推測しています。 @ closeed tho :) @ShawnMehan – Ned

+0

@Ned、私は完全なwhileループを終了しませんでした。私はそれの一部に取り組んだだけです...私はすでに推測されている手紙にどう対処するかを単に示していました。あなたは私にあなたのための完全なプログラムを書く必要がありますか?私はそれを解決したが、今は別の機能にそれらを分離することで立ち往生イム、それは大丈夫です –

+0

...作業が行われていない、それは明らかですので、あなたはプロジェクトとしてこれをやっている、あなたはヒントを望んでいたと思った、あなたを助けてくださいます私と一緒に? – Ned

関連する問題