私信  •  关注

Brian Bruggeman

Brian Bruggeman 最近创建的主题
Brian Bruggeman 最近回复了
6 年前
回复了 Brian Bruggeman 创建的主题 » Python:如何修复这个if语句错误?

只需在整个if表达式周围添加一个额外的括号。。。

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

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