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

时间:2019-07-26 22:02:00

标签: python python-3.x turtle-graphics

我的任务是创建一个十字和零的小游戏。但我只是一个初学者,所以我不知道如何解决此错误...

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 \ Profiles \ 12p506pq.default \ extensions {3d7eb24f-2740-49df-8937-200b1cc08f8a} \ defaults \ preferences \ hrastuku_noliki.py “,第163行,以del(hody [k-1]) IndexError:列表分配索引超出范围

1 个答案:

答案 0 :(得分:0)

此代码有多个设计问题,但直接的问题似乎是如何从列表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):

此循环控制游戏中的移动次数。最多只能有9个回合,但允许10个动作! (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()

还没有完成,仍然没有我们可以做到的那么干净,但是应该足够干净以构建下一个代码阶段:为游戏评分?