Использование нескольких выражений "или" with "и" - PullRequest
1 голос
/ 08 марта 2020
def check_over(mark):

    ######################################
    #This checks for a win################
    ######################################
    if board[1] == mark and board[2] == mark and board[3] == mark
    or board[4] == mark and board[5] == mark and board[6] == mark
    or board[7] == mark and board[8] == mark and board[9] == mark
    or board[1] == mark and board[4] == mark and board[7] == mark
    or board[2] == mark and board[5] == mark and board[8] == mark
    or board[3] == mark and board[6] == mark and board[9] == mark
    or board[1] == mark and board[5] == mark and board[9] == mark
    or board[3] == mark and board[5] == mark and board[7] == mark:
        print(f'{mark} won!')

Этот код выдает следующую ошибку, когда я пытаюсь вызвать его:

    if board[1] == mark and board[2] == mark and board[3] == mark
       ^
    SyntaxError: invalid syntax

Эта функция предназначена для проверки на выигрыш в c -ta c -то игра на основе списка python с именем board.

Где моя проблема?

РЕДАКТИРОВАТЬ: При попытке исправить код я получил еще одну ошибку, по-прежнему нужна помощь!

def check_over(mark):

    ######################################
    #This checks for a win################
    ######################################
    if board[1] == mark and board[2] == mark and board[3] == mark  or board[4] == mark and board[5] == mark and board[6] == mark or board[7] == mark and board[8] == mark and board[9] == mark or board[1] == mark and board[4] == mark and board[7] == mark or board[2] == mark and board[5] == mark and board[8] == mark or board[3] == mark and board[6] == mark and board[9] == mark or board[1] == mark and board[5] == mark and board[9] == mark or board[3] == mark and board[5] == mark and board[7] == mark:
        print(f'{mark} won!')
        return True
    elif " " not in board:
        ###############################
        #This checks for tie###########
        ###############################
        print('The game ended in a tie!')
        return True

Это дает мне следующую ошибку:

    def check_over(mark):
                         ^
IndentationError: expected an indented block

Ответы [ 2 ]

2 голосов
/ 08 марта 2020

Ваш SyntaxError происходит, потому что вы не используете \ символов для разбиения на несколько строк

def check_over(mark):
    ######################################
    #This checks for a win################
    ######################################
    if board[1] == mark and board[2] == mark and board[3] == mark \
    or board[4] == mark and board[5] == mark and board[6] == mark \
    or board[7] == mark and board[8] == mark and board[9] == mark \
    or board[1] == mark and board[4] == mark and board[7] == mark \ 
    or board[2] == mark and board[5] == mark and board[8] == mark \
    or board[3] == mark and board[6] == mark and board[9] == mark \
    or board[1] == mark and board[5] == mark and board[9] == mark \
    or board[3] == mark and board[5] == mark and board[7] == mark:
        print(f'{mark} won!')
1 голос
/ 08 марта 2020

У вас не может быть новых строк в середине вашей строки в python. Попробуйте следующее:

if board[1] == mark and board[2] == mark and board[3] == mark  or board[4] == mark and board[5] == mark and board[6] == mark or board[7] == mark and board[8] == mark and board[9] == mark or board[1] == mark and board[4] == mark and board[7] == mark or board[2] == mark and board[5] == mark and board[8] == mark or board[3] == mark and board[6] == mark and board[9] == mark or board[1] == mark and board[5] == mark and board[9] == mark or board[3] == mark and board[5] == mark and board[7] == mark:
    print(f'{mark} won!')
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...