编程就像搭积木

"""编程就像搭积木,本程序定义若干个函数,演示模块式编程方法"""

from turtle import *

def init_screen():
    """初始化屏幕对象"""
    s = Screen()
    s.setup(480,360)
    s.delay(0)
    s.title("画多边形")
    s.bgcolor("black")
    return s

def generate_turtle():
    """生成海龟对象"""
    t = Turtle()
    t.pensize(2)
    t.color("white","white")
    return t
    
def  draw_polygon(colorlist,海龟,边数,边长):
     """画多边形"""
     if  边数<3 or 边长==0:return
     amounts = len(colorlist)
     angle = 360/边数
     for i in range(边数):
         海龟.color(colorlist[i%amounts])
         海龟.fd(边长)
         海龟.rt(angle)
         
def main():
    """主程序"""
    
    colorlist = ['red','orange','yellow','green','cyan','blue','purple','pink']
    screen = init_screen()                       # 初始化屏幕对象
    g = generate_turtle()                        # 产生海龟对象
   
    for number in range(3,10):
       draw_polygon(colorlist,g,number,50)       # 画正多边形       
        

"如果不是作为模块导入而运行,那么就运行main函数。" 
if __name__ == "__main__":

    main()