Test a list

John Gordon gordon at panix.com
Wed Mar 20 16:39:15 EDT 2013


In <121be20a-c02a-4b0e-a86f-7c69597b9296 at googlegroups.com> =?ISO-8859-1?Q?Ana_Dion=EDsio?= <anadionisio257 at 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 at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list