Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的代码显示一个typeError:';功能';对象不可下标_Python_Python 3.x_Typeerror - Fatal编程技术网

Python 我的代码显示一个typeError:';功能';对象不可下标

Python 我的代码显示一个typeError:';功能';对象不可下标,python,python-3.x,typeerror,Python,Python 3.x,Typeerror,这就像我正在制作的一个游戏(一个简单的connect4游戏)的“scratch”代码,我只是在尝试我的功能 基本上,我得到的错误是TypeError:“function”对象在第25行不可下标。 这是我的密码: def play(): grid_height = int(input("Enter the number of rows:")) #Row grid_width = int(input("Enter the number of columns:")) #Width

这就像我正在制作的一个游戏(一个简单的connect4游戏)的“scratch”代码,我只是在尝试我的功能

基本上,我得到的错误是TypeError:“function”对象在第25行不可下标。 这是我的密码:

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()

这里有两个主要问题:

  • 您有一个名为
    grid
    的函数,并且经常使用名为
    grid
    的局部变量。这引起了一些混乱。重命名<代码>网格<代码> > <代码>创建网格>代码> < /p>
  • play()
    中调用
    grid
    (又称
    createGrid
    )时,您没有将返回值赋给变量。结果,您将函数
    grid
    传递到
    displayGrid
    updateGrid
    ,这将导致您看到的错误


  • 发帖后我意识到我的错误。我会更新帖子,但我现在有一个新问题哦,等等,你不能编辑帖子,我只会发布一个新帖子当发布一个关于产生异常的代码的问题时,总是包括完整的回溯-复制并粘贴它,然后将其格式化为代码(选择它并键入
    ctrl-k
    )@wwii哦!好的,谢谢!对不起,我是新来的。我下次再做!