如何修复 "list assignment index out of range" python3
How to fix "list assignment index out of range" python3
我的任务是创建一个十字和零的迷你游戏。但我只是一个初学者,所以我不知道如何解决这个错误...
import turtle
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = int()
j = 10
pl1 = turtle.Pen()
pl2 = turtle.Pen()
pole = turtle.Pen()
pl1.color("white")
pl2.color("white")
pole.color("white")
pole.goto(-170.00, 170.00)
pole.color("black")
pole.speed(0)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(99.99)
pole.right(90)
pole.fd(300)
pole.left(90)
pole.fd(99.99)
pole.left(90)
pole.fd(300)
pole.right(90)
pole.fd(100.02)
pole.right(90)
pole.fd(99.99)
pole.right(90)
pole.fd(300)
pole.left(90)
pole.fd(99.99)
pole.left(90)
pole.fd(301)
pole.color("white")
pole.setx(310)
for i in range(j):
p = str(input("Хто буде ходити першим X чи 0 ?: "))#who will go first?X or 0?
p = p.lower()
if p == "x":
k = int(input("Введіть клітинку: "))#user choose the number of area
if k in hody:
if (k == 1) and k in hody:
pl2.goto(-158.89, 158.89)
pl2.color("red")
pl2.goto(-78.89, 78.89)
pl2.color("white")
pl2.goto(-78.89, 158.89)
pl2.color("red")
pl2.goto(-158.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 2) and k in hody:
pl2.goto(-58.89, 158.89)
pl2.color("red")
pl2.goto(21.11, 78.89)
pl2.color("white")
pl2.goto(21.11, 158.89)
pl2.color("red")
pl2.goto(-58.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 3) and k in hody:
pl2.goto(40.89, 158.89)
pl2.color("red")
pl2.goto(121.11, 78.89)
pl2.color("white")
pl2.goto(121.11, 158.89)
pl2.color("red")
pl2.goto(40.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 6) and k in hody:
pl2.goto(40.89, 58.89)
pl2.color("red")
pl2.goto(121.11, -20.89)
pl2.color("white")
pl2.goto(121.11, 58.89)
pl2.color("red")
pl2.goto(40.89, -20.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 5) and k in hody:
pl2.goto(-59.89, 58.89)
pl2.color("red")
pl2.goto(21.11, -21.89)
pl2.color("white")
pl2.goto(21.11, 58.89)
pl2.color("red")
pl2.goto(-59.89, -21.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 9) and k in hody:
pl2.goto(40.89, -40.89)
pl2.color("red")
pl2.goto(121.11, -120.89)
pl2.color("white")
pl2.goto(121.11, -40.89)
pl2.color("red")
pl2.goto(40.89, -120.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 7) and k in hody:
pl2.goto(-158.89, -42.89)
pl2.color("red")
pl2.goto(-78.89, -122.89)
pl2.color("white")
pl2.goto(-78.89, -42.89)
pl2.color("red")
pl2.goto(-158.89, -122.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 8) and k in hody:
pl2.goto(-59.89, -40.89)
pl2.color("red")
pl2.goto(21.11, -121.89)
pl2.color("white")
pl2.goto(21.11, -40.89)
pl2.color("red")
pl2.goto(-59.89, -121.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 4) and k in hody:
pl2.goto(-158.89, 58.89)
pl2.color("red")
pl2.goto(-78.89, -20.89)
pl2.color("white")
pl2.goto(-78.89, 58.89)
pl2.color("red")
pl2.goto(-158.89, -20.89)
pl2.color("white")
del(hody[k - 1])
elif p == "0":
k = int(input("Введіть клітинку: "))# user choose the number of area
if k in hody:
if (k == 1) and k in hody:
pl1.goto(-119.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 4) and k in hody:
pl1.goto(-119.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 7) and k in hody:
pl1.goto(-119.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 2) and k in hody:
pl1.goto(-19.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 5) and k in hody:
pl1.goto(-19.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 8) and k in hody:
pl1.goto(-19.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 3) and k in hody:
pl1.goto(81.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 6) and k in hody:
pl1.goto(81.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 9) and k in hody:
pl1.goto(81.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
回溯(最近一次调用最后一次):文件 "C:\Users\user\AppData\Roaming\Profilesp506pq.default\extensions{3d7eb24f-2740-49df-8937-200b1cc08f8a}\defaults\preferences\hrastuku_noliki.py",第 163 行,在 del(hody[k - 1]) 中
IndexError: 列表赋值索引超出范围
这段代码有多个设计问题,但最直接的问题似乎是如何从列表中删除已玩过的方块 hody
:
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
方块被删除 index:
elif (k == 5) and k in hody:
# ...
del(hody[k - 1])
但是一旦删除了一个正方形,索引就会随着列表的缩小而改变!您可以简单地通过全局文本替换十几行半的行来解决这个问题:
del(hody[k - 1])
成为:
hody.remove(k)
现在我们按 值 删除方块,因此删除顺序无关紧要。但是还有其他问题,例如:
j = 10
# ...
for i in range(j):
这个循环控制游戏中的移动次数。最多只有九圈,但它允许十步! (j
范围从 0 到 9。)
让我们尝试合理化这段代码:
from turtle import Screen, Turtle
def draw_X(x, y):
pl2.penup()
pl2.goto(x, y)
pl2.pendown()
pl2.goto(x + 80, y - 80)
pl2.penup()
pl2.goto(x + 80, y)
pl2.pendown()
pl2.goto(x, y - 80)
def draw_0(x, y):
pl1.penup()
pl1.goto(x, y)
pl1.pendown()
pl1.circle(40)
pole = Turtle()
pole.hideturtle()
pole.speed('fastest')
pole.penup()
pole.goto(-170, 170)
pole.pendown()
for _ in range(4):
pole.forward(300)
pole.right(90)
for _ in range(2):
pole.forward(100)
pole.right(90)
pole.forward(300)
pole.left(90)
pole.forward(100)
pole.left(90)
pole.forward(300)
pole.right(90)
pole.forward(100)
pole.right(90)
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
pl1 = Turtle()
pl1.hideturtle()
pl1.color("blue")
pl2 = Turtle()
pl2.hideturtle()
pl2.color("red")
p = input("Хто буде ходити першим X чи 0 ?: ") # who will go first? X or 0?
while hody:
k = int(input("Введіть клітинку: ")) # user choose the number of area
if k in hody:
if p.lower() == "x":
if k == 1:
draw_X(-158.89, 158.89)
elif k == 2:
draw_X(-58.89, 158.89)
elif k == 3:
draw_X(40.89, 158.89)
elif k == 4:
draw_X(-158.89, 58.89)
elif k == 5:
draw_X(-59.89, 58.89)
elif k == 6:
draw_X(40.89, 58.89)
elif k == 7:
draw_X(-158.89, -42.89)
elif k == 8:
draw_X(-59.89, -40.89)
elif k == 9:
draw_X(40.89, -40.89)
p = "0" # successful move, switch players
elif p.lower() in ("0", 'o'):
if k == 1:
draw_0(-119.39, 79.44)
elif k == 2:
draw_0(-19.39, 79.44)
elif k == 3:
draw_0(81.39, 79.44)
elif k == 4:
draw_0(-119.39, -21.44)
elif k == 5:
draw_0(-19.39, -21.44)
elif k == 6:
draw_0(81.39, -21.44)
elif k == 7:
draw_0(-119.39, -121.44)
elif k == 8:
draw_0(-19.39, -121.44)
elif k == 9:
draw_0(81.39, -121.44)
p = "x" # successful move, switch players
hody.remove(k)
screen = Screen()
screen.mainloop()
仍未完成,仍未尽我们所能做到的干净,但应该足够干净以构建代码的下一阶段:游戏得分?
我的任务是创建一个十字和零的迷你游戏。但我只是一个初学者,所以我不知道如何解决这个错误...
import turtle
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
i = int()
j = 10
pl1 = turtle.Pen()
pl2 = turtle.Pen()
pole = turtle.Pen()
pl1.color("white")
pl2.color("white")
pole.color("white")
pole.goto(-170.00, 170.00)
pole.color("black")
pole.speed(0)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(300)
pole.right(90)
pole.fd(99.99)
pole.right(90)
pole.fd(300)
pole.left(90)
pole.fd(99.99)
pole.left(90)
pole.fd(300)
pole.right(90)
pole.fd(100.02)
pole.right(90)
pole.fd(99.99)
pole.right(90)
pole.fd(300)
pole.left(90)
pole.fd(99.99)
pole.left(90)
pole.fd(301)
pole.color("white")
pole.setx(310)
for i in range(j):
p = str(input("Хто буде ходити першим X чи 0 ?: "))#who will go first?X or 0?
p = p.lower()
if p == "x":
k = int(input("Введіть клітинку: "))#user choose the number of area
if k in hody:
if (k == 1) and k in hody:
pl2.goto(-158.89, 158.89)
pl2.color("red")
pl2.goto(-78.89, 78.89)
pl2.color("white")
pl2.goto(-78.89, 158.89)
pl2.color("red")
pl2.goto(-158.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 2) and k in hody:
pl2.goto(-58.89, 158.89)
pl2.color("red")
pl2.goto(21.11, 78.89)
pl2.color("white")
pl2.goto(21.11, 158.89)
pl2.color("red")
pl2.goto(-58.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 3) and k in hody:
pl2.goto(40.89, 158.89)
pl2.color("red")
pl2.goto(121.11, 78.89)
pl2.color("white")
pl2.goto(121.11, 158.89)
pl2.color("red")
pl2.goto(40.89, 78.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 6) and k in hody:
pl2.goto(40.89, 58.89)
pl2.color("red")
pl2.goto(121.11, -20.89)
pl2.color("white")
pl2.goto(121.11, 58.89)
pl2.color("red")
pl2.goto(40.89, -20.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 5) and k in hody:
pl2.goto(-59.89, 58.89)
pl2.color("red")
pl2.goto(21.11, -21.89)
pl2.color("white")
pl2.goto(21.11, 58.89)
pl2.color("red")
pl2.goto(-59.89, -21.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 9) and k in hody:
pl2.goto(40.89, -40.89)
pl2.color("red")
pl2.goto(121.11, -120.89)
pl2.color("white")
pl2.goto(121.11, -40.89)
pl2.color("red")
pl2.goto(40.89, -120.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 7) and k in hody:
pl2.goto(-158.89, -42.89)
pl2.color("red")
pl2.goto(-78.89, -122.89)
pl2.color("white")
pl2.goto(-78.89, -42.89)
pl2.color("red")
pl2.goto(-158.89, -122.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 8) and k in hody:
pl2.goto(-59.89, -40.89)
pl2.color("red")
pl2.goto(21.11, -121.89)
pl2.color("white")
pl2.goto(21.11, -40.89)
pl2.color("red")
pl2.goto(-59.89, -121.89)
pl2.color("white")
del(hody[k - 1])
elif (k == 4) and k in hody:
pl2.goto(-158.89, 58.89)
pl2.color("red")
pl2.goto(-78.89, -20.89)
pl2.color("white")
pl2.goto(-78.89, 58.89)
pl2.color("red")
pl2.goto(-158.89, -20.89)
pl2.color("white")
del(hody[k - 1])
elif p == "0":
k = int(input("Введіть клітинку: "))# user choose the number of area
if k in hody:
if (k == 1) and k in hody:
pl1.goto(-119.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 4) and k in hody:
pl1.goto(-119.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 7) and k in hody:
pl1.goto(-119.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 2) and k in hody:
pl1.goto(-19.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 5) and k in hody:
pl1.goto(-19.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 8) and k in hody:
pl1.goto(-19.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 3) and k in hody:
pl1.goto(81.39, 79.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 6) and k in hody:
pl1.goto(81.39, -21.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
elif (k == 9) and k in hody:
pl1.goto(81.39, -121.44)
pl1.color("blue")
pl1.circle(40)
pl1.fd(3)
pl1.color("white")
pl1.fd(30)
del(hody[k - 1])
回溯(最近一次调用最后一次):文件 "C:\Users\user\AppData\Roaming\Profilesp506pq.default\extensions{3d7eb24f-2740-49df-8937-200b1cc08f8a}\defaults\preferences\hrastuku_noliki.py",第 163 行,在 del(hody[k - 1]) 中 IndexError: 列表赋值索引超出范围
这段代码有多个设计问题,但最直接的问题似乎是如何从列表中删除已玩过的方块 hody
:
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
方块被删除 index:
elif (k == 5) and k in hody:
# ...
del(hody[k - 1])
但是一旦删除了一个正方形,索引就会随着列表的缩小而改变!您可以简单地通过全局文本替换十几行半的行来解决这个问题:
del(hody[k - 1])
成为:
hody.remove(k)
现在我们按 值 删除方块,因此删除顺序无关紧要。但是还有其他问题,例如:
j = 10
# ...
for i in range(j):
这个循环控制游戏中的移动次数。最多只有九圈,但它允许十步! (j
范围从 0 到 9。)
让我们尝试合理化这段代码:
from turtle import Screen, Turtle
def draw_X(x, y):
pl2.penup()
pl2.goto(x, y)
pl2.pendown()
pl2.goto(x + 80, y - 80)
pl2.penup()
pl2.goto(x + 80, y)
pl2.pendown()
pl2.goto(x, y - 80)
def draw_0(x, y):
pl1.penup()
pl1.goto(x, y)
pl1.pendown()
pl1.circle(40)
pole = Turtle()
pole.hideturtle()
pole.speed('fastest')
pole.penup()
pole.goto(-170, 170)
pole.pendown()
for _ in range(4):
pole.forward(300)
pole.right(90)
for _ in range(2):
pole.forward(100)
pole.right(90)
pole.forward(300)
pole.left(90)
pole.forward(100)
pole.left(90)
pole.forward(300)
pole.right(90)
pole.forward(100)
pole.right(90)
hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]
pl1 = Turtle()
pl1.hideturtle()
pl1.color("blue")
pl2 = Turtle()
pl2.hideturtle()
pl2.color("red")
p = input("Хто буде ходити першим X чи 0 ?: ") # who will go first? X or 0?
while hody:
k = int(input("Введіть клітинку: ")) # user choose the number of area
if k in hody:
if p.lower() == "x":
if k == 1:
draw_X(-158.89, 158.89)
elif k == 2:
draw_X(-58.89, 158.89)
elif k == 3:
draw_X(40.89, 158.89)
elif k == 4:
draw_X(-158.89, 58.89)
elif k == 5:
draw_X(-59.89, 58.89)
elif k == 6:
draw_X(40.89, 58.89)
elif k == 7:
draw_X(-158.89, -42.89)
elif k == 8:
draw_X(-59.89, -40.89)
elif k == 9:
draw_X(40.89, -40.89)
p = "0" # successful move, switch players
elif p.lower() in ("0", 'o'):
if k == 1:
draw_0(-119.39, 79.44)
elif k == 2:
draw_0(-19.39, 79.44)
elif k == 3:
draw_0(81.39, 79.44)
elif k == 4:
draw_0(-119.39, -21.44)
elif k == 5:
draw_0(-19.39, -21.44)
elif k == 6:
draw_0(81.39, -21.44)
elif k == 7:
draw_0(-119.39, -121.44)
elif k == 8:
draw_0(-19.39, -121.44)
elif k == 9:
draw_0(81.39, -121.44)
p = "x" # successful move, switch players
hody.remove(k)
screen = Screen()
screen.mainloop()
仍未完成,仍未尽我们所能做到的干净,但应该足够干净以构建代码的下一阶段:游戏得分?