Skip to content Skip to sidebar Skip to footer

How Do I Get This Code To Tell Me What Position A Word In The Sentence Is

varSentence = ('The fat cat sat on the mat') print (varSentence) varWord = input('Enter word ') varSplit = varSentence.split() if varWord in varSplit: print ('Found word')

Solution 1:

No need for a loop, use list.index, protected by a try/except block in case string is not found. list.index returns the first occurence of the word.

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

returns 3 because index found the word at the 3rd position (and arrays start at 0)


Solution 2:

Try with enumerate:

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

You can use the above like this:

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

Solution 3:

You just need to loop over position:

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

You can call the above function like this:

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

Example of output:

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

Solution 4:

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

Post a Comment for "How Do I Get This Code To Tell Me What Position A Word In The Sentence Is"