0

I am refactoring code for a Tic-Tac-Toe application in Python however i am running into trouble when rewriting the function that checks for a winning condition.

This is what i have right now

x = ('X', 'X', 'X')
o = ('O', 'O', 'O')

if ('X', '-', '-') == o or x:
    print(True)

This returns True even though the string shown is clearly not either one of the ones it's been compared to. However what is even weirder is that when I compare it to just one tuple

if ('X', '-', '-') == o:
    print(True)

True doesn't get returned. Can someone please explain why this happens

Leke
  • 35
  • 6

1 Answers1

1

Okay let's break this down. The way you have your code written, you have two things you're comparing to see if they're "truthy":

  • ('X', '-', '-') == o
  • x

The half of your or conditional is x itself. Since a tuple with values is considered truthy, your condition (everything after if) will always evaluate to true! If you're trying to compare ('X', '-', '-') to o and to x, you'll need to do this:

if ('X', '-', '-') == o or ('X', '-', '-') == x

AetherUnbound
  • 1,714
  • 11
  • 10