英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论