如何使用索引访问列表中的多个元素

How to use indexes to access multiple elements in a list

我目前正在为一个使用 turtle 模块的项目制作 TicTacToe。我根据用户给出的输入列出了乌龟应该去哪里的坐标。之后,我做了一个 if 语句,如果用户选择了某些点,那么用户就会赢。在制作 if 语句时,我使用索引来尝试查看用户的输入是否与实际坐标匹配。在检查 if 语句以查看特定电线是否匹配时。我不断收到如下错误:

TypeError: list indices must be integers or slices, not tuple

我在想是不是我写错了。我也知道我解释得不好,有什么问题请追问。我对编码和 python 也很陌生。我也写了会出现错误的地方。

下面是代码:

#Code for creating turtle and image as well

import turtle as trtl
TTT = trtl

wn = trtl.Screen()
wn.addshape('O2.gif')
wn.addshape('X3.gif')

TTT.pensize(5)
TTT.speed(0)


#VARIABLES
lines = 0
x = 50
y = -50 
Available_Cords = [(-100,100),(0,100),(100,100),(-100,0),(0,0),(100,0),(-100,-100),(0,-100),(100,-100)]
Taken_Cords = [(10,10)]
X_Used_Cords = [(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)]
Y_Used_Cords = [(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0),(0,0)]
Current_Turn = 2

#Code For creating background
while lines < 2:
TTT.penup()
TTT.goto(x,-150)
TTT.pendown()
TTT.goto(x,150)

TTT.penup()
TTT.goto(-150, y)
TTT.pendown()
TTT.goto(150, y)

x = x + -100 
y = y + 100
lines = lines + 1

TTT.penup()
while True:
#code for X turn
while (Current_Turn % 2 == 0): 
  User_Input = int(input("X, Please enter where you like to go"))
  Current_Cord = Available_Cords[User_Input - 1]
  
  
  #Checking if the spot chosen has been taken, if yes then the if code will run
  if Current_Cord in Taken_Cords:
     
     print("This Spot is not Available, Try another one")

  #If the spot is available, then the Else code will run which will place the image in the spot chosen
  else:
     TTT.shape('X3.gif')
     TTT.goto(Available_Cords[User_Input - 1])
     TTT.stamp()
     Taken_Cords.append(Available_Cords[User_Input - 1])

     #Code for Adding to list to detect if the player has won
     X_Used_Cords.insert(User_Input-1 ,Available_Cords[User_Input-1])
     print(X_Used_Cords)
     del X_Used_Cords[User_Input]
    
     
     
     Current_Turn = Current_Turn + 1

#LOTS OF IF STATEMENTS TO CHECK IF X WON
if X_Used_Cords[0:3] == Available_Cords[0:3]:
  print("Congratulations, you won :)")
  wn.mainloop()

if X_Used_Cords[3:6] == Available_Cords[3:6]:
  print("Congratulations, you won :)")
  wn.mainloop()   

if X_Used_Cords[6:9] == Available_Cords[6:9]:
  print("Congratulations, you won :)")
  wn.mainloop()


#CODE WHERE THE ERROR OCCURS

if X_Used_Cords[0,3,6] == Available_Cords[0,3,6]:
  print("Congratulations, you won :)")
  wn.mainloop()


#Code For Y Turn
while (Current_Turn % 2 != 0): 
  User_Input = int(input("Y, Please enter where you like to go"))
  Current_Cord = Available_Cords[User_Input - 1]
  
  
  if Current_Cord in Taken_Cords:
     print("This Spot is not Available, Try another one")
  
  else:
     TTT.shape('O2.gif')
     TTT.goto(Available_Cords[User_Input - 1])
     TTT.stamp()
     Taken_Cords.append(Available_Cords[User_Input - 1])
     Current_Turn = Current_Turn + 1

      #Code for Adding to list to detect if the player has won
     Y_Used_Cords.insert(User_Input-1 ,Available_Cords[User_Input-1])
     print(Y_Used_Cords)
     del X_Used_Cords[User_Input]

wn = trtl.Screen()
wn.mainloop()

错误在第 83 行:X_Used_Cords[0,3,6] == Available_Cords[0,3,6](可能还有其他错误)。

TypeError: list indices must be integers or slices, not tuple

此语法无效,因为您不能使用元组对列表进行切片 (0,3,6)。

而是分别获取坐标:

(X_Used_Cords[0] == Available_Cords[0]) and (X_Used_Cords[3] == Available_Cords[3]) and (X_Used_Cords[6] == Available_Cords[6])

或使用all:

all(X_Used_Cords[i] == Available_Cords[i] for i in (0,3,6))

或使用高级切片:

X_Used_Cords[0:7:3] == Available_Cords[0:7:3]

虽然这种情况只适用于索引均匀分布。