2017-11-10 6 views
-1

나는 진짜 모델에서 "word_to_guess"를 알지 못한다. 사용자의 입력 (char_guess)이 목록의 항목이라면 목록에서 문자의 위치를 ​​찾는 데 필요한 코드는 무엇입니까?파이썬에서 알 수없는 목록의 항목을 찾는 방법은 무엇입니까?

word_to_guess = 'cat' #usually random from user input list but 'cat' while testing 
listed_word = list(word_to_guess) 

char_guess = input("Enter a character you would like to guess ") 

테스트 케이스 :

word_to_guess = "cat" #using as an example although its unknown due to previous code at the bottom for reference 

listed_word = list(word_to_guess) #which is ['c', 'a', 't'] 

char_guess = input("Enter a character you would like to guess ") 
#user inputs 'a' and that is what char_guess is set to 

문자가 목록에있는 경우 (그것은이다) 내가 어떻게 그 문자의 위치를 ​​찾을까요? 내가 어떻게 '는이'이 부분 정말

import random 
word_choice_list = [ ] 
more_word = "no" 
word = input("Enter a word you would like to try and guess ") 
word_choice_list.append(word) 
print(" * this a yes or no question * ") 
more_word = input("Would you like to continue inputting words to guess? ") 
if more_word == "yes": 
    print("Please input a new word") 
    more_word = "yes" 
elif more_word == "no": 
    print("List of possible words is complete") 
    more_word = "no" 
else: 
    print('Incorrect answer, please try again') 
    more_word = "yes" 
while more_word == "yes": 
    word_2 = input("Enter a word you would like to try and guess ") 
    if word == word_2: 
     print("You have already inputted this word") 
    else: 
     word_choice_list.append(word_2) 
    print(" * this a yes or no question * ") 
    more_word = input("Would you like to continue inputting words to guess? ") 
    if more_word == "yes": 
     print("Ple input a new word") 
     more_word = "yes" 
    elif more_word == "no": 
     print("List of possible words is complete") 
     more_word = "no" 
    else: 
     print('Incorrect answer, please try again') 
     more_word = "yes" 

sr = random.SystemRandom() 
word_to_guess = sr.choice(word_choice_list) 

답변

-1

전에 [ 'C', 'A', 't']

이전 코드에서 어디에 찾을 것인가?

try: 
    i = listed_word.index(char_guess) 
except ValueError: 
    i = None 
+2

명백한 질문이라고 생각되면 중복 된 것을 찾아서 대답을 닫을 수 있도록 링크를 보여주십시오. 대답 (특히 "정말로?"이 유일한 텍스트 인 경우)을 작성하면 불필요한 혼란이 생깁니다. –

+0

정말 미안하지만 나는 문맥에서 다른 예제를 볼 때 코드 작성이 매우 어려워서 정말 고생하고있다. 도움을 주셔서 감사합니다 x – Megan59781

+0

@COLDSPEED - Ok) –