You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 3.11 TypeError排查:列表索引不能为元组(井字棋开发)

井字棋游戏错误:TypeError: list indices must be integers or slices, not tuple

错误原因

你写的grid[position,n]这种写法,Python会自动把position,n当成一个元组(哪怕没加括号,逗号分隔的两个值在索引位置就会被识别为元组),但列表的索引只能是整数或者切片,不能用元组,所以触发了这个错误。不止checkGridRow函数里有这个问题,checkGridCol函数里的grid[0,0]、grid[1,1]等写法也犯了同样的错。

另外还有个隐藏问题:你在函数里直接写game = False时,修改的是函数内部的局部变量,不会改变全局的game变量,就算判断出胜负,游戏也不会停止,得用global声明来解决。

解决方法

  1. 修正索引写法:把所有用逗号的索引改成两次方括号嵌套的形式,比如:
    • grid[position,n] → grid[position][n]
    • grid[0,0] → grid[0][0]
  2. 全局变量声明:在需要修改全局game变量的函数开头,加上global game,确保函数能修改全局的游戏状态。

修正后的完整代码

grid = [["_","_","_"],["_", "_", "_"],["_", "_", "_"]]
game = True


def checkGridRow(position):
    global game
    n = 0
    if grid[position][n]!= "_":
        if grid[position][n] == grid[position][n+1] and grid[position][n+1] == grid[position][n+2]:
            game = False
    return game

def checkGridCol():
    global game
    tempList = ""
    c1 = [grid[0][0], grid[1][1], grid[2][2]]
    c2 = [grid[2][0], grid[1][1], grid[0][2]]
    # 检查主对角线
    if "_" not in c1:
        tempList = "".join(c1)
        if tempList == "XXX" or tempList == "OOO":
            game = False
    # 检查副对角线(原代码漏了这部分)
    if "_" not in c2:
        tempList = "".join(c2)
        if tempList == "XXX" or tempList == "OOO":
            game = False
    return game
            
        
def PlayerTurnX():
    column = int(input("enter column >> 1,2,3: "))
    column = column -1
    while str(column+1) not in "123":
        column = int(input("enter column 1,2,3: "))
        column = column-1
    
    row = int(input("enter row >> 1,2,3: "))
    row = row-1

    while str(row+1) not in "123":
        row = int(input("enter row >> 1,2,3: "))
        row= row-1

    # 修正占位重复输入的逻辑(原代码只改了row,没改column)
    while grid[row][column] != "_":
        print("Space taken")
        row = int(input("enter row >> 1,2,3: "))
        row = row-1
        column = int(input("enter column >> 1,2,3: "))
        column = column-1
    
    grid[row][column] = "X"

    for item in grid:
        print(item[0]+" "+item[1]+" "+item[2])
    



def PlayerTurnO():
    column = int(input("enter column: >> 1,2,3: "))
    column = column-1
    while str(column+1) not in "123":
        column = int(input("enter column >> 1,2,3: "))
    
    row = int(input("enter row:  >> 1,2,3: "))
    row = row-1

    while str(row+1) not in "123":
         row = int(input("enter row:  >> 1,2,3: "))
         row = row-1

    # 修正占位重复输入的逻辑
    while grid[row][column] != "_":
        print("Space taken")
        row = int(input("enter row >> 1,2,3: "))
        row = row-1
        column = int(input("enter column>> 1,2,3: "))
        column = column-1
    
    grid[row][column] = "O"

    for item in grid:
        print(item[0]+" "+item[1]+" "+item[2])



while game:
    print("Player X, your turn!")
    PlayerTurnX()
    checkGridRow(0)
    checkGridRow(1)
    checkGridRow(2)
    checkGridCol()
    if not game:
        print("Player X wins!")
        break
    print("\n\n")
    print("Player O, your turn!")
    PlayerTurnO()
    checkGridRow(0)
    checkGridRow(1)
    checkGridRow(2)
    checkGridCol()
    if not game:
        print("Player O wins!")
        break

额外优化说明

  • 修正了玩家输入占位位置时的逻辑,原代码只重新输入了行或列,现在会同时重新输入行列,避免再次选到占位位置。
  • 新增了胜负后的提示信息,并且判断胜负后直接跳出循环,结束游戏。
  • 优化了对角线判断的代码,用"".join()代替循环拼接字符串,更简洁。

内容的提问来源于stack exchange,提问作者Ruben

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 10:36:59