这就像是我正在制作的游戏(简单的connect4游戏)的“草稿”代码,我只是在尝试我的功能。
基本上,我得到的错误是TypeError:第25行上的“ function”对象不可下标。 这是我的代码:
def play():
grid_height = int(input("Enter the number of rows:")) #Row
grid_width = int(input("Enter the number of columns:")) #Width
p1_char= 'e'
grid(grid_height,grid_width)
displayGrid(grid,grid_height,grid_width)
updateGrid(grid,grid_height,grid_width,p1_char)
displayGrid(grid,grid_height,grid_width)
def grid(grid_height,grid_width):
grid=[]
for row in range(grid_height): # FOR ROW
z =[]
for col in range(grid_width): # FOR COLUMN
z.append(" ")
grid.append(z)
return grid
def displayGrid(grid,grid_height,grid_width):
for row in range(grid_height):
for col in range(grid_width+1):
print("|" + grid[row-1][col-1],end = "")
print()
print(" "+" ".join([str(i) for i in range(1, 8+1)]))
return grid
def updateGrid(grid,grid_height,grid_width,p1_char):
move= int(input('Enter your move: '))
for i in range(grid_height+1):
grid[grid_height-1][move-1]= p1_char
break
return grid
play()
答案 0 :(得分:1)
这里有两个主要问题:
您有一个名为grid
的函数,并且经常使用名为grid
的局部变量。这引起了一些混乱。考虑将grid
重命名为createGrid
。
在grid
中调用createGrid
(又名play()
)时,没有将返回值分配给变量。结果,您最终将函数 grid
传递给displayGrid
和updateGrid
,这会导致您看到错误。