[newbie]search string in tuples

Brian Victor homeusenet1 at brianhv.org
Sat Aug 20 10:48:03 EDT 2005


Viper Jack wrote:
> but i want check on several object inside the tuple so i'm trying this:
>
> list=["airplane","car","boat"]

Note that this is actually a list, not a tuple as your subject suggests.
For the difference, take a look at this:
http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types

Also, it's generally considered bad form to shadow built-in type names
like list.  This prevents you from using methods in the list scope and
makes for potentially confusing bugs.

> while select != list[0] or list[1] or list[2]:

This actually behaves as though you wrote this:
while (select != list[0]) or list[1] or list[2]:

The since list[1] always evaluates to a true value (non-empty strings
are true), the while loop body will always execute.  Fortunately, python
has a very simple way of doing what you want:

vehicles = ("airplane", "car", "boat")
select = vars
while select not in vehicles:
    select=raw_input("Wich vehicle?")

-- 
Brian



More information about the Python-list mailing list