lists + string

Steve Holden sholden at holdenweb.com
Fri May 10 11:57:46 EDT 2002


"ian" <iwaters at btclick.com> wrote in message
news:UORC8.49610$7R.60091 at NewsReader...
> hi started to learn python this week
> and my lecturer threw us in the deep end
> and asked us to write a mail server.. oh well
>
> anyway in the program i read in some input from the user
>
> input = str(raw_input .........
>
As far as I know, raw_input() always returns a string, so you should just be
able to use

    input = raw_input("Search for: ")

> how do i compare it to an element in a list??
>
> ive tried
>
> for x in list
>     if input == x:print 'found item'
>
> but it dont work any help would be great thanx
>
Perhaps you don't have strings in your list? Impossible to say since you
don't show us how you form it, but...

>>> aList = ['1', 'hello', 'quit']
>>> myinput = raw_input("Search for: ") # input is a  builtin
Search for: hello
>>> for x in aList:
...   if x==myinput: print "Found", myinput
...
Found hello
>>>

Seems to work here.

regards
 Steve
--
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list