2017-11-10 8 views
-1

私は実際のモデルでは "word_to_guess"を知っていません。ユーザーの入力(char_guess)がリスト内の項目であれば、リスト内の文字の位置を調べるのにどのコードが必要でしょうか?Pythonで不明なリストのアイテムを見つけるには?

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) –

関連する問題