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

Alan Gauld alan.gauld@blueyonder.co.uk
Wed Jul 16 04:13:02 2003


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

THis says "if item equals the list" but item is single the list is
plural.
Instead try

 if item in list


>             elif item != list:

and similarly

 if item not in list

However using list as a variable name is bad since its also a python
function used to convert things to lists! Mabe 'pastimes' would be
a better name? Generally its a good idea to name variables after
the problem objects rather than to reflect their structure
(which might change!)

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld