[Tutor] multiple if and or statement

Marc Tompkins marc.tompkins at gmail.com
Mon Mar 14 21:00:35 CET 2011


On Mon, Mar 14, 2011 at 12:41 PM, Mike Franon <kongfranon at gmail.com> wrote:

> HI,
>
> I had a question, when running this small snippet of test code:
>
>
>
> a = ['test1', 'flag', 'monday']
>
> for i in a:
>    if i == 'test1' or 'test2':
>       print 'true'
>
>
> It always prints true
>
>
> $ ./testing.py
> true
> true
> true
>
>
> I know I am missing something, but in reality it should only print
> true once correct?
>
> No.  The string 'test2' (actually, ALL non-empty strings) evaluates to
True, so your condition will always be met.
Try this:

> if (i == 'test1') or (i == 'test2'):
>

or:

> if i in ('test1', 'test2'):
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110314/5734e575/attachment.html>


More information about the Tutor mailing list