Help

jacob Kruger jacob at blindza.co.za
Sun Feb 28 15:50:47 EST 2016


On 2016-02-28 9:58 PM, tomwilliamson115 at gmail.com wrote:
> I need to write a program that allows the user to enter a sentence then asks them which word they wish to find- and then tell them the position that word was within the sentence.
>
> E.g. Please help with this code
> Then they enter help it would return that it appears in the 2nd position in the sentence.
>
>>From what I gather it appears to be a list function but I am struggling to come up with a solution.
>
> Thanks.
>
Something along lines of loading the sentence into a string, using the 
str.split() function to split it into a list object, then cycling 
through to strip all the spaces and/or punctuation out of the 
elements/items, and then the list.index() function can return the index 
of a word in the list, which is the zero-based position of the item in 
that list.

#something along lines of
s = input("enter sentence")
s.replace(",", "") #do this for all common punctuation characters
l = s.split(" ") #space is actually default
s2 = input("enter word")
i = l.index(s2)
print("Your word is at position " + str(i+1) + " in the sentence")
#end code

That's just typed here in message - HTH

HTH

Jacob Kruger
Blind Biker
Skype: BlindZA
"Roger Wilco wants to welcome you...to the space janitor's closet..."



More information about the Python-list mailing list