[Tutor] multiple if and or statement

Mike Franon kongfranon at gmail.com
Mon Mar 14 21:13:41 CET 2011


Thank you everyone who responded, very fast responses I am impressed.

OK now I see where I went wrong and had to do


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


I guess I was thinking if I do


a = ['test1', 'flag', 'monday']
for i in a:

It would check each item in the list one at a time like a loop I was
thinking, so the first time it would test for 'test1', and the second
time it would test for 'flag', did not realize it would test the
entire list all at once.





On Mon, Mar 14, 2011 at 4:00 PM, Marc Tompkins <marc.tompkins at gmail.com> wrote:
> 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'):
>
>
>


More information about the Tutor mailing list