首页 文章

Python TicTacToe错误:TypeError:'function' object不可订阅

提问于
浏览
1

我刚刚开始学习Python,我正在尝试构建一个简单的tic tac toe游戏 . 但是我坚持这两个功能:

  • 使用第一个(choose_position())我想检查用户是否输入0到8之间的数字(该板由列表组成)

  • 使用第二个(check_if_can_x_o())如果板中的位置/点尚未填充,我想指定值'X' .

def choose_position():
    position = int(input("Choose the position of your sign:\n"))
    if position in range(0, 9):
        return position
    else:
        print("That is not in the board")

def check_if_can_x_o():
    position = choose_position()
    if board[position] == "X" or board[position] == "O":
        print("\nYou can't go there. Try again")
    else:
        board[position] = "X"

我将董事会定义如下:

board = [1, 2, 3, 4, 5, 6, 7, 8, 9]

def board():
        print("""
            {} | {} | {}
            ---------
            {} | {} | {}
            ---------
            {} | {} | {}
    """.format(board[0], board[1], board[2], board[3], board[4], board[5], board[6], board[7], board[8]))

我得到的错误如下:“TypeError:'function'对象不可订阅” .

非常感谢!

1 回答

  • 1

    嗨问题是你有一个阵列板和功能板,因此你需要重命名其中一个可能最容易将功能重命名为新名称,例如更换

    def board():
    

    def show_boards();
    

相关问题