简体   繁体   English

如何修复“列表分配索引超出范围”python3

[英]How to fix "list assignment index out of range" python3

I had the task to create a mini game of crosses and zeros.我的任务是创建一个十字和零的迷你游戏。 but I'm just a beginner so I do not know how to fix this error ...但我只是一个初学者,所以我不知道如何解决这个错误......

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])

Traceback (most recent call last): File "C:\\Users\\user\\AppData\\Roaming\\Profiles\\12p506pq.default\\extensions{3d7eb24f-2740-49df-8937-200b1cc08f8a}\\defaults\\preferences\\hrastuku_noliki.py", line 163, in del(hody[k - 1]) IndexError: list assignment index out of range回溯(最近一次调用):文件“C:\\Users\\user\\AppData\\Roaming\\Profiles\\12p506pq.default\\extensions{3d7eb24f-2740-49df-8937-200b1cc08f8a}\\defaults\\preferences\\hrastuku_noliki”, 163, in del(hody[k - 1]) IndexError: list assignment index out of range

This code has multiple design issues but the immediate one seems to be how played squares are removed from the list hody :这段代码有多个设计问题,但最直接的问题似乎是如何从列表中删除hody

hody = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Squares are removed by index :方块被索引删除:

elif (k == 5) and k in hody:
    # ...       
    del(hody[k - 1])

But once a square is removed, the indexes change as the list shrinks!但是一旦删除了一个方块,索引就会随着列表的缩小而改变! You can fix this simply by global text replacing the dozen and a half lines that read:您可以简单地通过全局文本替换以下十二行半来解决此问题:

 del(hody[k - 1])

to be:成为:

 hody.remove(k)

Now we're removing the squares by value so the order of removals doesn't matter.现在我们正在按删除方块,因此删除的顺序无关紧要。 But there are other problems, for example:但还有其他问题,例如:

j = 10
# ...
for i in range(j):

This loop controls the number of moves in the game.此循环控制游戏中的移动次数。 There are only nine turns maximum, but it allows for ten moves!最多只有九个回合,但它允许十个动作! ( j ranges from 0 through 9.) j范围从 0 到 9。)

Let's try to rationalize this code:让我们尝试合理化这段代码:

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()

Still not complete, and still not as clean as we could make it, but should be clean enough to build the next stage of the code: scoring the game?仍然不完整,仍然没有我们能做到的那么干净,但应该足够干净以构建下一阶段的代码:为游戏评分?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM