python 画任意多边形
urtle是Python内置的图形库,在这里,我们需要知道正多边形内角计算公式:内角=(边数-2)*180/边数
可选项:提示输入一种颜色,进行填充。本程序运行环境是Python3的IDLE。
import turtle ##python中的图形库 colors=["red","yellow","purple","blue","black","green"] turtle.pensize(2) turtle.setup(width=800, height=800, startx=400, starty=0) turtle.pencolor("white") #默认中心点开始,gisoracle turtle.goto(0,300) for num in range(3,20): turtle.pencolor(colors[num%6]) angle = 180 - (num - 2) * 180 / num for i in range(num): turtle.begin_fill() turtle.forward(100) turtle.right(angle) turtle.end_fill() turtle.done()