'how can i loop this code to make a pattern?
***I have an assignment to create a tile pattern. I finished the pattern, but I am struggling to get the code to repeat. I need to be able to run it with the following specs:
column_ct=10,row_ct=10
column_ct=16,row_ct=9
column_ct=100,row_ct=100
with column_ct and row_ct referring to the number of columns and rows respectively. my code is pretty simple as I am very new to coding. If someone could give me advice on how to implement my code into a loop, I would be very grateful. Here is my code:***
import turtle
def tile():
t = turtle.Turtle()
t.fillcolor("black")
t.begin_fill()
t.circle(20)
t.end_fill()
t.penup()
t.back(30)
t.fillcolor("red")
t.pendown()
t.begin_fill()
t.circle(50)
t.end_fill()
t.penup()
t.back(50)
t.pendown()
t.forward(100) # Forward turtle by 100 units
t.left(90) # Turn turtle by 90 degree
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
t.forward(100)
t.left(90)
column_ct= 10
row_ct=10
def tile_repeat (column_ct, row_ct,):
for r in range(row_ct):
for y in range(column_ct):
tile()
t.penup()
t.forward(100)
t.pendown
tile_repeat(10, 10)
turtle.done()
Solution 1:[1]
To loop the pattern into a grid shape you can do:
def tile_repeat (column_ct, row_ct,):
t = turtle.Turtle()
for r in range(row_ct):
for y in range(column_ct):
tile(t) # passing the turtle object to the function
t.penup()
t.forward(180)
t.pendown()
# return to left most part of drawings
t.penup()
t.right(180)
t.forward(100*column_ct)
# go down one row
t.left(90)
t.forward(100 )
t.left(90)
t.pendown()
tile_repeat(3, 3)
turtle.done()
Using this you have to pass the t variable to your function
def tile(t):
and remove the
t = turtle.Turtle()
inside of tile()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrew Ryan |
