社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
私信  •  关注

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