2016-10-25 3 views
-1

컴퓨터가 사전 정의 된 목록에서 단어를 선택하는 프로그램을 작성하려고합니다. 그런 다음 사용자가 문자를 하나씩 입력하여 단어를 추측 해 봅니다.루핑 문제 - 단어의 특정 문자 찾기

사용자가 정확하게 추측하는지 여부에 관계없이 단어의 문자 수만큼 추측을 유지할 수 있도록 프로그램을 반복하려고합니다.

그러나 몇 가지 이유로 프로그램은 현재 정확하게 추측하면 두 번만 반복되며 잘못 생각하면 전혀 반복되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

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

'컴퓨터 _ 선택 '이란 무엇입니까? 그리고 for 루프에서 코드를 들여 쓰지 않았습니다. –

+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는 이것을 고칠 것이다) –