Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eweka.nl!lightspeed.eweka.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'skip:[ 20': 0.03; 'syntax': 0.03; 'correct,': 0.09; 'iterate': 0.09; 'cc:addr :python-list': 0.10; 'index': 0.13; '-tkc': 0.16; 'accordingly': 0.16; 'comparison.': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'offset,': 0.16; 'pythonic': 0.16; 'value:': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'element': 0.17; '(or': 0.18; 'math': 0.20; 'otherwise,': 0.20; 'bit': 0.21; 'cc:2**0': 0.23; 'cc:no real name:2**0': 0.24; 'script': 0.24; 'pass': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'subject:list': 0.28; 'initial': 0.28; 'index,': 0.29; 'value)': 0.29; 'code': 0.31; 'running': 0.32; 'print': 0.32; 'equal': 0.33; 'likely': 0.33; 'list': 0.35; 'but': 0.36; 'compare': 0.36; 'test': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'your': 0.60; 'here': 0.65; 'sounds': 0.71; 'everything.': 0.84; 'received:50.22': 0.84; 'subject:Test': 0.84; 'ana': 0.91 Date: Wed, 20 Mar 2013 13:36:01 -0500 From: Tim Chase To: Ana =?UTF-8?B?RGlvbsOtc2lv?= Subject: Re: Test a list In-Reply-To: <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> References: <121be20a-c02a-4b0e-a86f-7c69597b9296@googlegroups.com> X-Mailer: Claws Mail 3.7.6 (GTK+ 2.20.1; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363804459 news.xs4all.nl 6990 [2001:888:2000:d::a6]:44183 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41613 On 2013-03-20 11:15, Ana Dion=C3=ADsio wrote: > t=3D [3,5,6,7,10,14,17,21] >=20 > 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: >=20 > while i<=3D25: > if i=3D=3Dt[]: > print "Test1" > else: > print "Test2" >=20 > What is missing here for this script work? Well, your code never increments "i", so it will loop forever; you also don't subscript "t" with anything, so you have invalid syntax there; you also don't have any values in the list that actually match their 0-indexed offset, so even if your code was correct, it would still (correctly) return Test2 for everything. This sounds a bit like homework, but the Pythonic way would likely iterate over the data and its enumeration: for index, value in enumerate(t): if index =3D=3D value: # compare index & value accordingly ... If you need to have the index start at 1 (or some other value) instead of 0, and you're running Python2.6+, you can pass the initial index to enumerate(). Otherwise, you have to do the math in your comparison. -tkc