あなたはstr
への入力を変換する必要はありません。それはすでに文字列です。
words
はあなたの意見ではありません。これはすべてsplit
メソッドへの参照です。あなたは実際にはsplit
と呼ぶことはありません。
-
>>> a_string = "Look a string"
>>> print(a_string.split)
>>> <built-in method split of str object at (memory address)>
が分割を呼び出すのに対し、我々は持っている:
>>> print(a_string.split())
>>> ['Look', 'a', 'string']
それはあなたがwords = sntc.split
以下列挙し、すべてを達成しようとしているまさに不明です。私は実際にsntc
を列挙し、指定されたwrd
がこの分割リストの項目と一致するかどうかをチェックしたいと思う。別の問題は、反復可能で反復可能と項目に戻りインデックスを列挙し、
あります、ここでのインデックス(位置)はちょうどこのリスト内の位置になります(ワード)+ 1
ない実際の位置文中の。
-
>>> sntc = input("Please input a sentence with no punctuation: ")
Please input a sentence with no punctuation: Look a sentence
>>> wrd = input("Please input a word from that sentence: ")
Please input a word from that sentence: a
>>> words = sntc.split()
単語= ここでは 'a'の位置は1です。あなたが実際に欲しい
>>> for i, word in enumerate(words):
... if word == wrd:
... print("Chosen word is in position %d" % (i + 1))
...
Chosen word is in position 2 #Position of 'a' + 1
は次のとおりです。
for word in words:
if word == wrd:
print("Position is %d" % sntc.index(word))