2016-10-29 8 views
0

私は単純なハングマンゲームをデザインしようとしていますが、現在このゲームをループする際に問題があります。私はPythonにはまったく初心者ですが、おそらくこれにはかなり単純な問題があることを認識していますが、助けていただければ幸いです。ここで ハングマンゲーム - ループでの問題

は、私は現在、ゲームを持っているコードです:

import random 
random_words = ["stationery", "notepad", "pencil", "paper","eraser","highlighter","stapler","sharpener"] 
computer_choice = random.choice(random_words) 
print("The number of letters in the word I have chosen is " +   str(len(computer_choice) + ".") 

player_guess = None 
guessed_letters = [] 
word_guessed = [] 

for letter in computer_choice: 
    word_guessed.append("-") 
    joined_word = None 

player_guess = str(input("Please pick a letter you think is in the word I have chosen.")) 

attempts = (len(computer_choice)-1) 

for letter in (0, len(computer_choice)): 

if attempts != 0 and "-" in word_guessed: 
    joined_word = "".join(word_guessed) 
    print(joined_word) 

    guessed_letters.append(player_guess) 

    for letter in range(len(computer_choice)): 
      if player_guess == computer_choice[letter]: 
       word_guessed[letter] = user_input 

      if player_guess not in computer_choice: 
       attempts -= 1 
       player_guess = str("Please try again. You have " + str(attempts) + " attempts remaining.") 

if "-" not in word_guessed: 
print("Congratulations! {} was the word").format(computer_choice) 

else: 
print("Unlucky! The word was " + str(computer_choice) + "!") 

現在、ゲームがループしない、と単純にストレート「不運、言葉は___だった」にカットします。これをどうやって解決するのですか?どうした?

+0

を推測する単語内の文字を検索と置換する別の方法を作成しましたか?例えば、word_guessed: 'のif attempts!= 0と' - 'は 'for(0、len(computer_choice)):'の文字の直下に現れ、最後の 'if/else'のボックも同様にボルケージされます最初の一見では、少なくとも) – BorrajaX

答えて

1

コードを投稿するときは、理解しやすく、インタプリタの中核となるようにPythonをコンパイルするように、identを付けてください。

あなたのコードではループのいくつかのエラーがありました。forループを使用して単純な繰り返しを繰り返したり、リストを循環させたりすることはありません。簡単な繰り返しのために。 2つのネストされたforループ内の同じ変数も複雑になります。

また、ループ内のすべての文字を循環させる必要もなく、inオペレータはすでに文字が存在するかどうかを単語で確認します。

私は、もう少し簡単な私は、あなたのインデントをダブルチェックでし

import random 
random_words = ["stationery", "notepad", "pencil", "paper","eraser","highlighter","stapler","sharpener"] 
computer_choice = random.choice(random_words) 
print("The number of letters in the word I have chosen is " + str(len(computer_choice))) 
win = computer_choice #computer_choice will be destroyed later on 
print(computer_choice) 
guessed_letters = [] 
word_guessed = [] 

for letter in computer_choice: 
    word_guessed.append("-") 
joined_word = None 

player_guess = input("Please pick a letter you think is in the word I have chosen.") 

attempts = (len(computer_choice)+1) 

x = 0 
while x < len(computer_choice): 
    x+=1 

    if attempts != 0 and "-" in word_guessed: 

     if player_guess in computer_choice: 
      y=0 
      while y < computer_choice.count(player_guess): #will count how many times guessed word is in string 
       y+=1 
       pos = computer_choice.find(player_guess) #return index of where the letter is 
       word_guessed[pos] = player_guess #replaces word_guessed with letter in pos 
       computer_choice = computer_choice.replace(player_guess,'#',1) #deletes so it won't find it again 
      player_guess = "/" 
     if player_guess not in computer_choice: 
      attempts -= 1 
      print("Please try again. You have " + str(attempts) + " attempts remaining.") 
     player_guess = input("Please pick a letter you think is in the word I have chosen.") 
     joined_word = "".join(word_guessed) 
     print(joined_word) 
    else: 
     break 

if "-" not in word_guessed: 
    print("Congratulations! %s was the word"%win) 

else: 
    print("Unlucky! The word was " + str(win) + "!")