[newbie]search string in tuples

V

Viper Jack

Hi all,
i'm new to python programming so excuseme if the question is very stupid.
here the problem.
this code work

list=["airplane"]
select=vars
while select != list[0]:
select=raw_input("Wich vehicle?")

but i want check on several object inside the tuple so i'm trying this:

list=["airplane","car","boat"]
select=vars
while select != list[0] or list[1] or list[2]:
select=raw_input("Wich vehicle?")

but loops and doesn't want work.
I have tried with other methods (for) but nothings.
I haven't find nothing on this topic, so i asked here.
Thanks in advance.
 
B

Brian Victor

Viper said:
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?")
 
S

Scott David Daniels

Viper said:
Hi all,
i'm new to python programming so excuseme if the question is very stupid.
here the problem.
this code work

list=["airplane"]
select=vars
while select != list[0]:
select=raw_input("Wich vehicle?")

but i want check on several object inside the tuple so i'm trying this:

list=["airplane","car","boat"]
select=vars
while select != list[0] or list[1] or list[2]:
select=raw_input("Wich vehicle?")

but loops and doesn't want work.
I have tried with other methods (for) but nothings.
I haven't find nothing on this topic, so i asked here.
Thanks in advance.


Normally you'd define a function doing the search and use it.
Don't be afraid to use more lines if your code is clear.

First, avoid using list as a name, it is already defined.
Second, None is the standard constant not really anything value.

def missing(element, source):
for part in source:
if element == part:
return False
return True

vehicles = ["airplane", "car", "boat"]
select = None
while missing(select, vehicles):
select = raw_input("Which vehicle?")


For this particular test, there happens to be an idiom you can use:

vehicles = ["airplane", "car", "boat"]
select = None
while select not in vehicles:
select = raw_input("Which vehicle?")


--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top