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
This code has multiple design issues but the immediate one seems to be how played squares are removed from the list 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.)
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.