-1

I'm a newbie and I'm doing the classic Tic Tac Toe exercise on Python, I wanted to use more than one variable in an if, like this:

if boarda[1],boarda[2],boarda[3]='X':
   print ('player X wins)

The output is Syntax error, is it possible to do this? and if it is how can I write it? Big thanks

I want to check against and and or, so the duplicate is invalid.

Alec
  • 8,529
  • 8
  • 37
  • 63
Vadino
  • 53
  • 5

1 Answers1

1

Well, you could always do the intuitive

if boarda[1] == 'X' or boarda[2] == 'X' or boarda[3] == 'X'

But a far better way to approach this is to use the in operator:

if 'X' in (boarda[1], boarda[2], boarda[3])

For a long list you'd probably want a list comprehension:

if 'X' in [boarda[x] for x in range(1, 4)]

For checking against and, it's even easier:

if boarda[1] == boarda[2] == boarda[3] == 'X'

For arbitrary types:

vals = (boarda[1], boarda[2], boarda[3])
if all(v == 'X' for v in vals)
Alec
  • 8,529
  • 8
  • 37
  • 63
  • The problem is I need it to be True when three of them are 'X' and those just function like an or(if one of them is true then true) I could do if boarda[1] == 'X' and boarda[2] == 'X' and boarda[3] == 'X' but that'd be way too long for all the cases(8 possible winning cases) isn't there a shorter way? Thanks for replying so fast – Vadino May 22 '19 at 23:55
  • Great! Thanks a lot, the fourth will do it for me, it's about the same length as what I was using, but the correct syntax of course – Vadino May 23 '19 at 00:03