2016-04-07 1 views

答えて

0

あなただけのはずlist.index()使用:

sentence = input("What is your sentence? ") 
List1 = sentence.split(' ') 
word = input("What word do you want to find? ") 

print(List1.index(word)) 
+0

を私持っすでにこれをトライが、単語がリストに複数回表示された場合、それは動作しません。 –

0

あなたのコードは、例外処理を追加する必要があります場合wordにリストに見つからない:

sentence = input("What is your sentence? ") 
List1 = sentence.split() 
word = input("What word do you want to find? ") 
try: 
    print List1.index(word) 
except Exception, e: 
    print str(e) 
0

あなたは単語が分割されたリストを使用enumerateにある場合のすべての出現する場合:

sentence = input("What is your sentence? ") 
List1 = sentence.split(" ") 
word = input("What word do you want to find? ") 
print([i for i, w in enumerate(List1) if w == word]) 
関連する問題