解决这个问题的一个好方法是使用参数定义一个圆,然后只使用你想要的。此外,由于六边形是重复的,因此您可以使用 for 循环为其构造许多边。这是我解决问题的方法。
from turtle import *
setup()
x = 200
# Use your own value
y = 200
# Use your own value
def circles (radius, colour):
penup()
pencolor (colour)
goto (0,radius)
pendown ()
setheading (180)
circle (radius)
penup()
circles (100, "red")
circles (50, "yellow")
circles (25, "green")
def hexagon (size_length):
pendown ()
forward(size_length)
right (60)
goto (x, y)
for _ in range (6):
hexagon (50)
exitonclick ()
这样,您不必继续定义圆,只需添加自己的参数,就可以使用 for 循环轻松完成六边形。
A good way to go about this is to define a circle with parameters and just use what you want. Also since a hexagon is repetitive, you can use a for loop to construct a lot of the sides for it. Here is how I solved it.
from turtle import *
setup()
x = 200
# Use your own value
y = 200
# Use your own value
def circles (radius, colour):
penup()
pencolor (colour)
goto (0,radius)
pendown ()
setheading (180)
circle (radius)
penup()
circles (100, "red")
circles (50, "yellow")
circles (25, "green")
def hexagon (size_length):
pendown ()
forward(size_length)
right (60)
goto (x, y)
for _ in range (6):
hexagon (50)
exitonclick ()
With this you don't have to keep defining circle and just add your own parameters and the hexigon can be easily done with a for loop.
发布评论
评论(1)
解决这个问题的一个好方法是使用参数定义一个圆,然后只使用你想要的。此外,由于六边形是重复的,因此您可以使用 for 循环为其构造许多边。这是我解决问题的方法。
这样,您不必继续定义圆,只需添加自己的参数,就可以使用 for 循环轻松完成六边形。
A good way to go about this is to define a circle with parameters and just use what you want. Also since a hexagon is repetitive, you can use a for loop to construct a lot of the sides for it. Here is how I solved it.
With this you don't have to keep defining circle and just add your own parameters and the hexigon can be easily done with a for loop.