我需要创建一个Python脚本,它允许您将一个单词放入其中,并在您所做的句子中找到该单词的位置

我需要创建一个Python脚本,它允许您将一个单词放入其中,并在您所做的句子中找到该单词的位置,python,Python,我已经开始这样做了,到目前为止它工作得很好,但是无论我使用什么方法,它都找不到一个单词的位置。我已经研究过了,到目前为止还没有发现任何东西,请帮忙 print ("Enter your sentence here.") #This will ensure that the person knows to input a sentence. sentence = input () #This will allow the person to input a sentence. s = senten

我已经开始这样做了,到目前为止它工作得很好,但是无论我使用什么方法,它都找不到一个单词的位置。我已经研究过了,到目前为止还没有发现任何东西,请帮忙

print ("Enter your sentence here.")
#This will ensure that the person knows to input a sentence.
sentence = input ()
#This will allow the person to input a sentence.
s = sentence.split()
#This will allow me to split the sentence.
positions = [s.index(x)+1 for x in s]
#This will change the first position from 0 to 1.
print ("Which word would you like to find in this sentence?")
#This will allow the person to write a word in their sentence.
word = input ()
#This allows the person to input the word.
print (positions)

您可以在末尾添加额外的索引位置。

您还可以使用enumerate打印单词及其在句子中的位置:

print "enter sentence"
sentence=raw_input()
print "enter word"
word=raw_input()
s=sentence.split()
for index, w in enumerate(s):
    if w==word:
        print w, index
可能的重复
print "enter sentence"
sentence=raw_input()
print "enter word"
word=raw_input()
s=sentence.split()
for index, w in enumerate(s):
    if w==word:
        print w, index