-1

Disclaimer: I'm a bit of a newbie so this may very well be just a stupid oversight of mine (although, I suppose that would make this issue easier to solve)

When I run the following code:

xoro = [0 for i in range(9)]
checkeD = [False for i in range(9)]
potplay = 0

def checkeR(box_x): #This checks if a move has been played in a given box.
    global checkeD
    print(xoro[int(box_x)]) #prints "0", as no augmentations were made to xoro since declaration
    if xoro[int(box_x)] == 1 or 4:
        checkeD[int(box_x)] = True

checkeR(0)
print(checkeD[0])

What should happen is when xoro[0] is passed through the if statement, since xoro[0] = 0 and does not fit the if statement's requirements, checkeD[0] should remain false, yet when run, the code returns checkeD[0] = True. What's more interesting is that when we remove the or operator, making the if statement if xoro[int(box_x)] == 1:, the code works as expected, returning False.

I have absolutely no idea what is going on it it would be great if someone helped.

  • If you're trying to compare `xoro[int(box_x)]` to both 1 and 4 you should use something like: `xoro[int(box_x)] == 0 or xoro[int(box_x)] == 4` Because currently your expression is being evaluated as `xoro[int(box_x)] == 1 or bool(4)` and `bool(4)` is always `True` – Henry Ecker May 23 '21 at 01:42
  • As an aside, `global checkeD` is totally pointless and should be removed – juanpa.arrivillaga May 23 '21 at 02:00

1 Answers1

1

Sadly if statements don't work like that. You must compare it to both possibilities.

if xoro[int(box_x)] == 1 or xoro[int(box_x)] == 4:

But because you are using or and not and you can use in.

if xoro[int(box_x)] in [1,4]:
Buddy Bob
  • 5,829
  • 1
  • 13
  • 44