I would like to create the diagram below using the for, while, if syntax, but I don't understand the code very well.Could you please let me know?
A 2x2 grid line is drawn like a rice field, and an equilateral triangle line is drawn from each midpoint of the four sides on the outer periphery.
The code I wrote looks like the one below.
import turtle def draw4square(t,size):
for i in range (4):
for i in range (3):
t.forward (size)
t.left(90)
t.forward (size)
figure you want to create
python
I think there are several ways to write it, but I will start by correcting the comments you wrote.
I think the following is a fragment of the code you gave me in the comment.
def draw4square(t,size):
for i in range (4):
for i in range (3):
t.forward (size)
t.left(90)
t.forward (size)
"From the figure to the point where I noticed ""4 loops"", it was good, but I think it was difficult to control the direction of the turtle."
There are three modifications to the code.
def draw4square(t,size):
# Unused subscripts (i) are accepted with _
for_in range(4):
# There are four sides, so you have to forward four times to write a square.
for_in range(4):
t.forward (size)
t.left(90)
# square once and turn
t.left(90)
However, the red triangle in the figure seems stronger than the square.If you look at only one square and a triangle, you will be able to write a stroke, so let's draw this all at once.
import turtle
def draw_triangle(t,size):
# a triangular drawing of three sides
for correct in (-30, 120, 120):
t.right(rect)
t.forward (size)
# to put something back in its original direction
t.right (150)
def draw4pieces(t,size):
t.pensize(3)
t. begin_fill()
for_in range(4):
# Only one side of the square is drawn first.
t.color('blue','green')
t.forward (size)
# At the end of the line, a triangle is drawn instead of a red pen.The size is appropriate.
t.color('red','green')
draw_triangle(t,size*1.5)
# Finally, it returns to the origin of the triangle, so it draws the rest of the square.
t.color('blue','green')
for_in range(3):
t.right (90)
t.forward (size)
# When the turtle returned, it turned right from the beginning.
# It's 90 degrees lying down, so it's OK!
t=turtle.Turtle()
draw4pieces(t,100)
t.end_fill()
US>turtle.done()
© 2024 OneMinuteCode. All rights reserved.