Python 函数不返回任何内容 - 井字游戏

Python function not returning anything - Tic Tac Toe

下面这段代码的问题是,除了 print 语句之外,check_win() 函数没有返回任何内容我可以通过放弃 while 循环中的所有逻辑而不使用函数来解决这个问题,但我想要这个代码要crystal清楚,有什么建议吗?

board = {
    'a1': '', 'a2': '', 'a3': '',
    'b1': '', 'b2': '', 'b3': '',
    'c1': '', 'c2': '', 'c3': ''
}

player_one = True

player_one_choice = input("Play player one: ")
if player_one_choice in board:
    board[player_one_choice] = 'x'
    player_one = False

elif player_one_choice not in board:
    print("Wrong input")
print(board)

checkwin = False


def check_win():
    """
    'x' check win logic
    """
    # Vertical check logic
    if 'x' in board['a1'] and 'x' in board['a2'] and 'x' in board['a3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['b1'] and 'x' in board['b2'] and 'x' in board['b3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['c1'] and 'x' in board['c2'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True

    # Horizontal check logic
    elif 'x' in board['a1'] and 'x' in board['b1'] and 'x' in board['c1']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a2'] and 'x' in board['b2'] and 'x' in board['c2']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a3'] and 'x' in board['b3'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True

    # Diagonal check logic
    elif 'x' in board['a1'] and 'x' in board['b2'] and 'x' in board['c3']:
        print("congrats x won")
        checkwin == True
    elif 'x' in board['a3'] and 'x' in board['b2'] and 'x' in board['c1']:
        print("congrats x won")
        checkwin == True

    elif 'o' in board['a1'] and 'o' in board['a2'] and 'o' in board['a3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['b1'] and 'o' in board['b2'] and 'o' in board['b3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['c1'] and 'o' in board['c2'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True

    # Horizontal check logic
    elif 'o' in board['a1'] and 'o' in board['b1'] and 'o' in board['c1']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a2'] and 'o' in board['b2'] and 'o' in board['c2']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a3'] and 'o' in board['b3'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True

    # Diagonal check logic
    elif 'o' in board['a1'] and 'o' in board['b2'] and 'o' in board['c3']:
        print("congrats o won")
        checkwin == True
    elif 'o' in board['a3'] and 'o' in board['b2'] and 'o' in board['c1']:
        print("congrats o won")
        checkwin == True

    return check_win


while True:
    check_win()

    player_two_choice = input("PLay player two: ")

    if player_two_choice != 'x' and player_one_choice in board:
        board[player_two_choice] = 'o'
        player_one = True
        print(board)
    if player_two_choice != 'x' and player_one_choice not in board or player_two_choice == 'a' or player_two_choice == 'b' or player_two_choice == 'c':
        print("Wrong input")

    check_win()

    if checkwin:
        break

    player_one_choice = input("Play player one: ")

    if player_one_choice in board:
        board[player_one_choice] = 'x'
        player_one = False

    elif player_one_choice not in board:
        print("Wrong input")
        break

    print(board)

这是我收到的“输出”


>>>Play player one: a1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': '', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>PLay player two: b1
{'a1': 'x', 'a2': '', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>Play player one: a2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': '', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>PLay player two: b2
{'a1': 'x', 'a2': 'x', 'a3': '', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
>>>Play player one: a3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': '', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
>>>PLay player two: b3
{'a1': 'x', 'a2': 'x', 'a3': 'x', 'b1': 'o', 'b2': 'o', 'b3': 'o', 'c1': '', 'c2': '', 'c3': ''}
congrats x won
>>>Play player one: 
Wrong input
    
Process finished with exit code 0

您拼错了 checkwin 变量

改变这个:

return check_win

对此:

return checkwin

第 84 行