Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103661

Re: Help

From jacob Kruger <jacob@blindza.co.za>
Newsgroups comp.lang.python
Subject Re: Help
Date 2016-02-28 22:50 +0200
Message-ID <mailman.25.1456693051.9760.python-list@python.org> (permalink)
References <6cdcd063-1c22-4f08-900c-f3260a543018@googlegroups.com>

Show all headers | View raw


On 2016-02-28 9:58 PM, tomwilliamson115@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..."

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help tomwilliamson115@gmail.com - 2016-02-28 11:58 -0800
  Re: Help jacob Kruger <jacob@blindza.co.za> - 2016-02-28 22:50 +0200
    Re: Help tomwilliamson115@gmail.com - 2016-02-29 04:53 -0800
      Re: Help Joel Goldstick <joel.goldstick@gmail.com> - 2016-02-29 08:05 -0500
        Re: Help Grant Edwards <invalid@invalid.invalid> - 2016-02-29 15:49 +0000
      Re: Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-02-29 13:41 +0000
      Re: Help Tom P <werotizy@freent.dd> - 2016-03-04 22:06 +0100
        Re: Help Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-03-04 21:20 +0000

csiph-web