如何让这段代码告诉我句子中的单词的位置是什么

时间:2016-12-13 13:11:55

标签: python

varSentence = ("The fat cat sat on the mat")

print (varSentence)

varWord = input("Enter word ")

varSplit = varSentence.split()

if varWord in varSplit:
    print ("Found word")
else:
    print ("Word not found")
    for (num, x) in enumerate(sentence):
    if word == x:
        print ("Your word is in position",num,"!")

4 个答案:

答案 0 :(得分:2)

不需要循环,使用list.index,受try/except块保护,以防找不到字符串。 list.index返回该单词的第一个出现。

sent = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY.'
words = sent.split()
word = "WHAT"

try:
    print(words.index(word)+1)
except ValueError:
    print("{} is not in the sentence".format(word))

返回3,因为index在第3个位置找到了这个词(并且数组从0开始)

答案 1 :(得分:0)

您只需要循环position

def word_position(word):
    for i in position:
        if word == i[1]:
            return i[0]
    return "Your word is not in the sentence"

您可以像这样调用上述函数:

word = input('Which word would you like to search for?').upper()
pos = word_position(word)

输出示例:

>>> sent = 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY.'
>>> sen1 = sent.split()
>>> 
>>> position = enumerate(sen1)
>>> 
>>> word = input('Which word would you like to search for?').upper()
Which word would you like to search for?'what'
>>> word
'WHAT'
>>> 
>>> pos = word_position(word)
>>> print(pos)
2

答案 2 :(得分:0)

sent= 'ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY.' # This defines the variable 'sent'
sen1= sent.split() # This splits the sentence into individual words
print (sen1) # This tells the program to display the sentence as the individual words
Word= str(input('Which word would you like to search for?')) # Tells the user to input one of the words from the sentence that they would like to find
Word1= Word.upper() # Changes the user's input to block capitals
print ("Searching for ", Word1) # Prints the user's input in capitals
position = list(enumerate(sen1))  # Numbers the words in the sentence starting at (ASK, 0)

if Word1 in sen1: # Asks the program to check if the user's input is within the defined sentence
    for word in position:
        if word[1]==Word1:
            print ('Your word is in the sentence at ' , word[0]+1) # Tells the program to print a certain sentence if the users' inputted word is within the sentence
else: # Begins to tell the program what to do if the if condition is not met
    print ('Your word is not in the sentence') # Tells the program what to do print if the users' inputted word is not within the sentence

答案 3 :(得分:0)

尝试enumerate

for i in enumerate(varSplit):
    if i[1] == varWord:
        print(i[0])

您可以像这样使用上述内容:

varSentence = ("The fat cat sat on the mat")

varWord = input("Enter word ")

varSplit = varSentence.split()

if varWord in varSplit:
    for i in enumerate(varSplit):
        if i[1] == varWord:
            print("Your word is in position", i[0], "!")
            break  # To stop when the first position is found!
else:
    print ("Word not found")