gpt4 book ai didi

Python 井字游戏错误 : TypeError: 'function' object is not subscriptable

转载 作者:行者123 更新时间:2023-12-01 09:21:20 32 4
gpt4 key购买 nike

我刚刚开始学习Python,我正在尝试构建一个简单的井字棋游戏。但是我被困在这两个函数上:

-对于第一个(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'对象不可下标”。

非常感谢!

最佳答案

您好,问题是您有一个阵列板和功能板,因此您需要重命名其中一个,这可能是最容易将功能重命名为新名称的方法,例如替换

def board():

def show_boards();

关于Python 井字游戏错误 : TypeError: 'function' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50774987/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com