[Tutor] How to "grab" a word in a sentance,in Python (fwd)

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Tue Jul 15 14:56:01 2003


Hi everyone,


I'm forwarding Paul's message to the list --- I'm actually slightly busy
at the moment, but I'll try getting to it later today.


Quick response: I recommend trying your program with just one keyword:
have it respond to just 'golf' for the moment.  We can always improve it
later to make it respond to both 'golf' and 'tennis'.


Paul, don't worry about my time; we're all volunteers here.  *grin* But
make sure you're sending your replies to the list at 'tutor@python.org',
so that even if I'm out of commission, the other tutors can help you.


Good luck!


---------- Forwarded message ----------
Date: Tue, 15 Jul 2003 19:11:45 +0100
From: Paul Hickey <pauljhickey@eircom.net>
To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] How to "grab" a word in a sentance,in Python

Thanks Danny, sorry about this I think I jumped in too quick!! But I find
that in the end its the best way!!You learn more quicker and have fun!
Sorry to take your time .

Im a bit wide of the mark it seems, what I wrote ( in my naivety!) is :
    #def list ():
            list = ['golf' 'tennis' ]
            item = raw_input("What is your favourite pastime")
            if item == list:
                print '',item.',is boring!!! Get a life!!''
            elif item != list:
           print 'never did,',item,'sounds pretty cool!'
The "key" word could be anywhere in the sentence, ie" I like swimming" or "
I do like to go swimming"
Embarrassingly that's it!!
Hey im a Newbie.. I would happily let this go only im havin fun doin this!
The rest of the program is pretty simple but works, if i can crack this ill
be a happy dude for awhile! And impress the hell out of my friends.!
Tell me to get off your case , and go back to simple stuff, I can take a
hint
Thanks for your time
Paul Hickey



----- Original Message -----
From: "Danny Yoo" <dyoo@hkn.eecs.berkeley.edu>
To: "Paul Hickey" <pauljhickey@eircom.net>
Cc: <tutor@python.org>
Sent: Tuesday, July 15, 2003 6:34 PM
Subject: Re: [Tutor] How to "grab" a word in a sentance,in Python


>
>
> On Tue, 15 Jul 2003, Paul Hickey wrote:
>
> > Hi , im trying to "grab" a word or two in a program im trying to write
> > in Python, eh im a newbie! to Python and programming.
> >
> > Something like " input.... What is your favourite hobby?
> >                         Answer.... I like to play golf.
> >   With a reply    print "never played golf, sounds boring!!!!"
> >
> > I tried to put the "key" word in a list but could not get it to work,
> > ive tried some other ideas to no avail. Any pointers would be great.
>
>
> Hi Paul,
>
>
> To check to see if a keyword lives in a sentence or not, we can use a
> string's find() method.  Here's a brief description of its documentation:
>
>
> ###
> >>> help(''.find)
>
> Help on built-in function find:
>
> find(...)
>     S.find(sub [,start [,end]]) -> int
>
>     Return the lowest index in S where substring sub is found,
>     such that sub is contained within s[start,end].  Optional
>     arguments start and end are interpreted as in slice notation.
>
>     Return -1 on failure.
> ###
>
>
> The last part of the description is really useful: it says that as long as
> find() doesn't return -1, we can find the substring within the larger
> string fine.
>
>
> For example, here's a sample interpreter session that shows a little what
> find() does:
>
> ###
> >>> s = 'this is a test'
> >>> s.find('zork!')
> -1
> >>> s.find('is')
> 2
> >>> s.find('his')
> 1
> ###
>
>
> Try a few examples one your own with find(), just to get a sense of what
> it's doing.
>
>
> By the way, the last find() example shows a problem we can run into: we
> might find our keyword embedded within a larger word.  ('his' is a
> substring of 'this').  That's something we'll need to fix, but we can come
> back to it later.
>
>
> This should be enough for you to write a simple program to detect a
> keyword in a sentence; show us what you've tried so far, and we can make
> comments for improvement.  And please feel free to ask questions; we'll be
> happy to chat about this.
>
>
> Good luck to you!
>
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>