Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41621

Re: Test a list

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Test a list
Date 2013-03-20 20:39 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <kid6pj$aua$1@reader2.panix.com> (permalink)
References <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com>

Show all headers | View raw


In <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> =?ISO-8859-1?Q?Ana_Dion=EDsio?= <anadionisio257@gmail.com> writes:


> t= [3,5,6,7,10,14,17,21]

> Basically I want to print Test 1 when i is equal to an element of the
> list "t" and print Test 2 when i is not equal:

> while i<=25:

>     if i==t[]:

>        print "Test1"

>     else:

>        print "Test2"

> What is missing here for this script work?

1. You're missing an initial value for i.
2. You never increment i, so the while loop will never exit.
3. The syntax 'if i==t[]' is wrong.
4. The way you have this code set up, you would need two loops: an outer
   loop for i from 1 to 25, and an inner loop for the items in t.
5. As others have said, this is a poor way to go about it.  You want to
   use "if i in t".

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Test a list Ana DionĂ­sio <anadionisio257@gmail.com> - 2013-03-20 11:15 -0700
  Re: Test a list timothy crosley <timothy.crosley@gmail.com> - 2013-03-20 11:24 -0700
  Re: Test a list Joel Goldstick <joel.goldstick@gmail.com> - 2013-03-20 14:26 -0400
  Re: Test a list Tim Chase <python.list@tim.thechases.com> - 2013-03-20 13:36 -0500
  Re: Test a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-20 18:40 +0000
  Re: Test a list John Gordon <gordon@panix.com> - 2013-03-20 20:39 +0000

csiph-web