2020-09-28

How do I create a chessboard using Python and nested loops?

I am trying to create a chessboard using nested loops with Python. I am having a hard time figuring out how to fill specific boxes with black and how to create 64 boxes. My coding so far is:

import turtle

t = turtle.Turtle()
t.speed(0)
t.penup() 
t.goto(0, 0)
t.pendown() 

for j in range(-150, 100, 50):
    for i in range(-150, 150, 50):
        t.penup()
        t.goto(i, j)
        t.pendown()
        t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.color("black")
            t.end_fill()

for j in range(-100, 150, 50):
    for i in range(-100, 150, 50):
        t.penup()
        t.goto(i, j)
        t.pendown()
        t.begin_fill()
        for k in range(4):
            t.forward(50) 
            t.left(90) 
            t.end_fill()
            t.hideturtle()

turtle.done()


from Recent Questions - Stack Overflow https://ift.tt/2HyyPf7
https://ift.tt/eA8V8J

No comments:

Post a Comment