Python基础《七段数码管的绘制》

#七段数码管的绘制.py
#导入库模块

from turtle import *
from random import *

#绘制单段间隔
def drawGap():
    penup()
    fd(5)
    
#绘制单段数码管
def drawLine(draw):
    drawGap()
    if draw:
        pendown()
    else:
        penup()
    fd(40)
    drawGap()
    right(90)
#根据数字绘制七段数码管
def drawDigit(digit):
    pencolor(random(),random(),random())
    drawLine(True) if digit in [2,3,4,5,6,8,9] else drawLine(False)  #用第1段的数字
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,1,3,4,5,6,7,8,9] else drawLine(False)  #用第2段的数字
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,3,5,6,8,9] else drawLine(False)  #用第3段的数字
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,6,8] else drawLine(False)  #用第4段的数字
    pencolor(random(),random(),random())
    left(90)
    drawLine(True) if digit in [0,4,5,6,8,9] else drawLine(False)  #用第5段的数字
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,2,3,6,7,8,9] else drawLine(False)  #用第6段的数字
    pencolor(random(),random(),random())
    drawLine(True) if digit in [0,1,2,3,4,7,8,9] else drawLine(False)  #用第7段的数字
    pencolor(random(),random(),random())
    left(180)
    penup()
    fd(20)
#获得要输出的数字

#主函数(设置画布与画笔等)

#调用主函数
经验分享 程序员 微信小程序 职场和发展