2016-10-25 8 views
-1

コンピュータがあらかじめ定義されたリストから単語を選び、次に単語を推測するために文字を1つずつ入力するプログラムを作成しようとしています。ループの問題 - 単語内の特定の文字を見つける

私は、ユーザーが正しく推測するかどうかにかかわらず、単語の文字数と同じ回数だけ推測できるようにプログラムをループしようとしています。

しかし、何らかの理由で、プログラムは現在、正しく推測すると2回だけループし、間違っていれば全く反応しません。私は間違って何をしていますか?

user_input = str(input("Please pick a letter you think is in the word I have chosen.")) 
for i in (0, len(computer_choice)) #computer_choice is the word the computer has generated 
    if user_input in WordList: 
     user_input = str(input("You got one of the letters! Keep going!")) 
    else: 
     user_input = str(input("You did not get one of the letters. Please try again. You have " + str(i) + " attempts left.")) 
+0

に入るまでのコードのこの作品は、ゲームループ? – Psytho

+0

'computer_choice'とは何ですか?そして、あなたはforループのコードを字下げしなかった。 –

+1

あなたのコードは1文字しか推測しない。複数の文字を推測したい場合は、それをループで繰り返す必要があります。 –

答えて

0

何をする必要があると、このような変数「コンピュータの選択」に保存されている単語、何かの長さまで0から実行forループ内のユーザーからランダム文字の推測を尋ねることです。

100%ではないことを確認
for i in range(0, len(computer_choice)) #computer_choice is the word the computer has generated 
    user_input = str(input("Please pick a letter you think is in the word I have chosen.")) 

    if user_input in computer_choice: 
     print "You got one of the letters! Keep going!" 
    else: 
     print "You did not get one of the letters. Please try again. You have " + str(len(computer_choice)-i-1) + " attempts left." 
+0

また、 "...あなたは" + str(len(computer_choice)-i-1)+ "試行錯誤"と書いた最後の行に加えられた変更にも注意してください。残りの選択肢はlen(computer_choice) - i-1です。 – PJay

0
import random 

ChosenWord = random.choice(["WordOne", "WordTwo"]) 

while True: 
    user_input = str(input("Enter letter: ")) 

    if user_input.lower() in ChosenWord.lower(): 
     print("Correct!") 
    else: 
     print ("You Lost!") 
     break 

何を後にしているが、ユーザが不正な文字 `computer_choice`は何ですか

+0

**。lower **を追加しました。大文字と小文字は区別されません。また、文字列を入力しているので、文字列(tryとexceptはこれを修正する) –

関連する問題