TypeError: 列表索引必须是整数或切片,而不是元组 (Python 3.11)

huangapple go评论61阅读模式
英文:

TypeError: list indices must be integers or slices, not tuple (Python 3.11)

问题

在你提供的Python代码中,你遇到的问题在于以下这行代码:

if grid[position,n]!= "_":

这里你尝试使用grid[position, n]来访问二维列表grid中的元素,但Python中的二维列表不支持使用逗号来索引元素。正常情况下,应该使用两个方括号,例如grid[position][n]

你可以将上述行代码修改为:

if grid[position][n] != "_":

这样应该就能解决这个问题。这个问题是由于使用逗号而不是方括号来索引列表元素引起的。

英文:

I am trying to create a noughts and crosses game in python, and I got an error in the bold text area in checkGridRow(). This is where I want to check if the game has been won by any player by checking for "XXX" or "OOO" in a horizontal row. At the end of the code, I use the parameter of "position" as the Y position in the grid and so pass 0, 1 and 2 to check all the rows. However I have run into the error in the title, and I don't know what it means despite searching, as I have no tuples in my code (as far as I can see). I am a beginner so please try to explain in relatively simple terms, thank you for helping

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


def checkGridRow(position):
    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():
    tempList = ""
    c1 = [grid[0,0], grid[1,1], grid[2,2]]
    c2 = [grid[2,0], grid[1,1], grid[0,2]]
    if not any("_" in i for i in c1):
       for var in c1:
           tempList+= var
       if tempList == "XXX":
           game = False
       elif 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


    
    if grid[row][column]=="_":
        grid[row][column] = "X"
    elif grid[row][column]!= "_":
        print("Space taken")
        row = int(input("enter row >> 1,2,3: "))
        row = row-1



    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

    if grid[row][column]=="_":
        grid[row][column] = "O"
    else:
        print("Space taken")
        column = int(input("enter column>> 1,2,3: "))
        column = column-1
        n=n-1



    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()
    print("")
    print("")
    print("Player O, your turn!")
    PlayerTurnO()
    checkGridRow(0)
    checkGridRow(1)
    checkGridRow(2)
    checkGridCol()

I've tried searching the error message and still cannot figure out where the tuple is, as far as I know tuples look like this myTuple = (x, y, z)

答案1

得分: 3

grid[position,n] 不是访问嵌套列表的正确语法。

请使用 grid[position][n]

英文:

grid[position,n] is not the correct syntax for accessing a nested list.

Use grid[position][n] instead.

huangapple
  • 本文由 发表于 2023年2月19日 23:03:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75501052.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定