[newbie]search string in tuples
Scott David Daniels
Scott.Daniels at Acm.Org
Sat Aug 20 13:26:37 EDT 2005
Viper Jack wrote:
> 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
Scott.Daniels at Acm.Org
More information about the Python-list
mailing list