My program's purpose is to find positions of iterations of words in a sentence, and the malfunctioning subroutine goes as follows.
def analyse(splitString):
wordToSearch = input("What word are you searching for instances of? ").lower()
for word in splitString:
positionLibrary = ""
positionInSplitString = 0
instances = 0
if word == wordToSearch:
position = splitString.index(word)
positionLibrary += str(position)
print (position, word)
instances += 1
positionInSplitString += 1
return (positionLibrary, instances, wordToSearch)
Let "splitString" be the list form of the sentence "THE ALTERATION OF MOTION IS EVER PROPORTIONAL TO THE MOTIVE FORCE IMPRESSED AND IS MADE IN THE RIGHT LINE ON WHICH THAT FORCE IS IMPRESSED". Now, say I search for "impressed" in splitString, it returns What word are you searching for instances of? impressed
11 impressed
11 impressed
['the', 'alteration', 'of', 'motion', 'is', 'ever', 'proportional', 'to', 'the', 'motive', 'force', 'impressed', 'and', 'is', 'made', 'in', 'the', 'right', 'line', 'on', 'which', 'that', 'force', 'is', 'impressed']
wordToSearch impressed
instances 1
positionLibrary 11
which tells me that the program somehow knows that there are 2 instances of "impressed" yet does not count the number of these into the "instances" variable (which seems unreliable and doesn't work.) positionLibrary, which is meant to store (as a string) a record of the positions of instances found, doesn't work. I believe this is because the program only returns the position of the FIRST INSTANCE of "impressed" as shown in 11 impressed
11 impressed
.
Now, how would I make the program actually return any positions after the first instance of a word and make the "instances" variable work? I've searched far and wide and have not found a solution.