1

I am still new at python so I was just wondering if it is possible to write if statements like this:

def win_check(board, mark):

    for mark in board:
        if (mark == board [7] and mark == board [8] and mark == board [9]) or
            (mark == board [4] and mark == board [5] and mark == board [6]) or
            (mark == board [1] and mark == board [2] and mark == board [3]):
            print("you won 1") 
        elif (mark == board [7] and mark == board [4] and mark == board [1]) or
            (mark == board [8] and mark == board [5] and mark == board [2]) or
            (mark == board [9] and mark == board [6] and mark == board [3]):
            print("you won 2")
        elif (mark == board [7] and mark == board [5] and mark == board [3]) or
            (mark == board [9] and mark == board [5] and mark == board [1]) or
            (mark == board [1] and mark == board [2] and mark == board [3]):
            print("you  won 3")
        else:
            print ("game tied")

This is a function from a Tic-tac-toe game. As i try to run the function, it gives me an error stating

 File "<ipython-input-13-94777e972072>", line 4
    if (mark == board [7] and mark == board [8] and mark == board [9]) or
                                                                         ^
SyntaxError: invalid syntax

I'm not too sure on what this means and what is required, can anyone assist me on this and help me on my method?

KevinG
  • 882
  • 3
  • 16
  • 33
mckee.98
  • 11
  • 1
  • 2
    Possible duplicate of [Styling multi-line conditions in 'if' statements?](https://stackoverflow.com/questions/181530/styling-multi-line-conditions-in-if-statements) – KevinG Mar 14 '19 at 17:50

3 Answers3

0

You can't end line before statement w\o putting '\' at the end of it. The proper way to do it is:

for mark in board:
    if (mark == board[7] and mark == board[8] and mark == board[9]) or\
            (mark == board[4] and mark == board[5] and mark == board[6]) or\
            (mark == board[1] and mark == board[2] and mark == board[3]):
        print("you won 1") ...

And so on..

I've also removed spacing before indexing board, so it should be board[i] and not board [i]

Good luck!

hodisr
  • 314
  • 2
  • 12
0

Your problem is that the if statement needs to be one continuous line. Or python treats the next line as new code. You need a backslash to escape the end of line and continue with the line under it.

for mark in board:
    if (mark == board[7] and mark == board[8] and mark == board[9]) \
            or (mark == board[4] and mark == board[5] and mark == board[6]) \
            or (mark == board[1] and mark == board[2] and mark == board[3]):

        print("you won 1")

    elif (mark == board[7] and mark == board[4] and mark == board[1]) \
            or  (mark == board[8] and mark == board[5] and mark == board[2]) \
            or  (mark == board[9] and mark == board[6] and mark == board[3]):
        print("you won 2")
    elif (mark == board [7] and mark == board[5] and mark == board[3]) \
            or  (mark == board[9] and mark == board[5] and mark == board[1]) \
            or (mark == board[1] and mark == board[2] and mark == board[3]):
        print("you  won 3")
    else:
        print ("game tied")
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
0

Just add an extra parenthesis around the entire if expression...

if (
       ((a == b) and (b == c) and (c == d))
       or ((e == f) and (f == g) and (g == h))
       or ...
       ):
   # do something here
   print('Winner')

That said, this is probably the wrong way entirely for you to do the code you're actually attempting. A much more idiomatic approach might be as follows. Keep in mind you only need to check if the current player actually won after that player moved.

def detect_winner(win_value: str = 'o') -> bool:
    """Determines if current player won.

    Args:
        win_value: the value to check for a win; value of current player

    Returns:
        True if winner is detected otherwise False
    """
    # Anything within a parenthesis can allow for newlines and ignore
    #  generally considered "good" and "pythonic" whitespacing
    row_win = (
        all(cell == win_value for cell in mark[1:3])
        or all(cell == win_value for cell in mark[4:6])
        or all(cell == win_value for cell in mark[7:9])
        )
    col_win = (...)
    cross_win = (...)
    possible_wins = (row_win, col_win, cross_win)
    found_a_winner = True if any(possible_wins) else False
    return found_a_winner
Brian Bruggeman
  • 5,008
  • 2
  • 36
  • 55