2017-06-15 7 views
0

ここに私のコードがあります。私を助けてください。ハングマンで同じ言葉を2回使用する方法

word = input("What's your word?\n").upper() 
 
forguess = " ".join(word) 
 
char = "_" 
 
for index in range(len(word)): 
 
    word = word[:index] + char + word[index+1:] 
 
spacedwords = " ".join(word) 
 
print(spacedwords) 
 

 
chances = 5 
 

 
while chances != 0: 
 
    print("You have " + str(chances) + " chances left\n") 
 
    letter = input("Guess the letters: \n").upper() 
 
    if letter in forguess:    #check if the input letter is in original word 
 
     pos = forguess.index(letter) #gets the letter position from forguess original word 
 
     spacedwords = spacedwords[:pos] + letter + spacedwords[pos+1:] #replace word in the blank ones 
 
     print(spacedwords) 
 
     if spacedwords == forguess: 
 
      print("YOU WON!!!") 
 
      break 
 
    else: 
 
     chances -= 1 
 
     if chances == 0: 
 
      print("YOU LOOSE!!!")

答えて

2

あなたのコードが動作しない主な理由は、あなたが手紙を推測する際spacedwords変化しながら、forguessにはない、ということです。このようにして、index呼び出しでは、常に最初の出現番号がletterになります。あなたが望むものを達成する方法の例は、ここでは、(これを行うには、より効率的な方法が存在することになる)可能な限り小さなコードを変更しようとすると:

word = input("What's your word?\n").upper() 
forguess = " ".join(word) 
char = "_" 
for index in range(len(word)): 
    word = word[:index] + char + word[index+1:] 
spacedwords = " ".join(word) 
print(spacedwords) 

chances = 5 

while chances != 0: 
    print("You have " + str(chances) + " chances left\n") 
    letter = input("Guess the letters: \n").upper() 
    if letter in forguess:    #check if the input letter is in original word 
     positions = [i for i,x in enumerate(forguess) if x == letter] #gets all letter positions from forguess original word 
     for pos in positions: 
      spacedwords = spacedwords[:pos] + letter + spacedwords[pos+1:] #replace all blank positions with letter 
     print(spacedwords) 
     if spacedwords == forguess: 
      print("YOU WON!!!") 
      break 
    else: 
     chances -= 1 
     if chances == 0: 
      print("YOU LOOSE!!!") 

EDIT

は、最初の通りOPのやり方を誤解していた時代、元のコードをできるだけ修正しようとしている例がここにあります。最初にforguessと同じ新しい変数originalを導入しました。今度は、文字が正しく推測されるたびに、forguessが変更され(推測された文字はNULL文字に変更されます)、次回の検索で再び表示されなくなります。そして、最後にspaceedwordsforguessの代わりにoriginalと比較します。

word = input("What's your word?\n").upper() 
forguess = " ".join(word) 
original = " ".join(word) 
char = "_" 
for index in range(len(word)): 
    word = word[:index] + char + word[index+1:] 
spacedwords = " ".join(word) 
print(spacedwords) 

chances = 5 

while chances != 0: 
    print("You have " + str(chances) + " chances left\n") 
    letter = input("Guess the letters: \n").upper() 
    if letter in forguess:    #check if the input letter is in original word 
     pos = forguess.index(letter) #gets the letter position from forguess original word 
     spacedwords = spacedwords[:pos] + letter + spacedwords[pos+1:] #replace word in the blank ones 
     forguess = forguess[:pos] + chr(0) + forguess[pos+1:] 

     print(spacedwords) 
     if spacedwords == original: 
      print("YOU WON!!!") 
      break 
    else: 
     chances -= 1 
     if chances == 0: 
      print("YOU LOOSE!!!") 

:あなたは、単一の文字のリストにあなたのforguessを変換した場合

全体の問題は、処理するためにはるかに簡単だろう。

+0

あなたの修正コードは、すべての文字を同時に与えています。一度に1つずつ取得する方法はありませんか? –

+0

ああ、私はコードがどう動くべきか誤解しました。私はそれに応じて解決策を調整します。 –

+0

@ SujanJoshi私の答えは編集をご覧ください。 –

関連する問題