我的代码显示typeError:“函数”对象不可下标

时间:2019-09-24 16:39:14

标签: python python-3.x typeerror

这就像是我正在制作的游戏(简单的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()

1 个答案:

答案 0 :(得分:1)

这里有两个主要问题:

  1. 您有一个名为grid的函数,并且经常使用名为grid的局部变量。这引起了一些混乱。考虑将grid重命名为createGrid

  2. grid中调用createGrid(又名play())时,没有将返回值分配给变量。结果,您最终将函数 grid传递给displayGridupdateGrid,这会导致您看到错误。

    < / li>