-1

I'm trying to create a program which requires any one of eight lists to equal a separate list. I have several list variables: testC1, testC2, etc and xwin, as well as one string: result It's hard to explain, so here's the code:

result = ("n")
print ("testD1 is:",str(testD1)) #Debugging
print ("xwin is:",str(xwin)) #Debugging
if (testC1 or testC2 or testC3 or testR1 or testR2 or testR3 or testD1 or testD2) == (xwin):
    result = ("x")
else:
    pass
print (result) #Debugging

And when the code is run, I get the following result:

>>> testD1 is: ['x', 'x', 'x']
>>> xwin is: ['x', 'x', 'x']
>>> n

I would expect to get "x" as the result, as I only want to check if one (or more) of the lists (in this case, testD1)to be identical to 'xwin'.

I know it would be possible through a series of if, elif statements, but I'm sure there must be some way to do it without doing that preferrably. I'm just not sure on the syntax in this situation.

Any help would be much appreciated.

  • 1
    Use `any` with a list comprehension – dawg Jun 19 '16 at 00:30
  • 2
    Your logic in the if statement is not what you are expecting to happen. What you want is something along the lines of `if (testC1 == xwin) or (testC2 == xwin) or ...`. The logic you currently have is actually combining the lists `testC1, testC2, ...` and comparing them to `xwin`. – user2027202827 Jun 19 '16 at 00:36
  • You may have more issues than this. Are you guaranteed to have 8 lists, or is that the max number of lists? – Thecor Jun 19 '16 at 00:40
  • 1
    Python isn't English. Writing `(a or b) == c` is very specific: It evaluates the logical-OR condition of `a` with `b`, then compares the result to `c`. It is very different from `a == c or b == c`, which is what you want. – Tom Karzes Jun 19 '16 at 00:43

1 Answers1

1

A non-empty list evaluates to True in boolean contexts, so for example if you have:

([] or ['a', 'b', 'c'] or ['x', 'x', 'x']) == (['x', 'x', 'x'])

the left part of the condition will result in ['a', 'b', 'c'] that is not equal to ['x', 'x', 'x'].

Then to do what you want you could replace

(testC1 or testC2 or testC3 or testR1 or testR2 or testR3 or testD1 or testD2) == (xwin)

by

xwin in (testC1, testC2, testC3, testR1, testR2, testR3, testD1, testD2)