How do you use a for loop in Python to draw a five-pointed star?

You can draw a pentagon using a for loop with the following code.

import turtle

# 创建画布和画笔
t = turtle.Turtle()
s = turtle.Screen()

# 设置画笔的颜色和大小
t.pensize(2)
t.pencolor("black")

# 循环画五角星
for i in range(5):
    t.forward(100)
    t.right(144)

# 隐藏画笔
t.hideturtle()

# 显示画布
s.mainloop()

Running the above code will draw a pentagram on the canvas.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds