python小练习----七段数码管

废话不多说直接上代码:

import turtle,datetime
def drawgrape():# 画一段间隙
    turtle.penup()
    turtle.fd(5)
def draw_dight(draw):#画一段数字管
    drawgrape()
    turtle.pendown() if draw else turtle.penup()
    turtle.fd(40)
    drawgrape()
    turtle.right(90)

def draw_sevendight(num):#画一个七段数字管
    '''
    1:5,4
    2:6,7,1,4,3
    3:6,7,1,2,3
    4:5,1,7,2
    5:6,5,1,2,3
    6:6,5,1,2,3,4
    7:6,7,2
    8:1,2,3,4,5,6,7
    9:5,6,7,1,2,3
    0:5,6,7,2,3,4
    '''
    draw_dight(True) if num in[2, 3, 4, 5, 6, 8, 9] else draw_dight(False)# 1
    draw_dight(True) if num in[1, 3, 4, 5, 6, 7, 8, 9, 0] else draw_dight(False)# 2
    draw_dight(True) if num in[2, 3, 5, 6, 8, 9, 0] else draw_dight(False)# 3
    draw_dight(True) if num in[2, 6, 8, 0] else draw_dight(False)# 4
    turtle.left(90)
    draw_dight(True) if num in[4, 5, 6, 8, 9, 0] else draw_dight(False)# 5
    draw_dight(True) if num in[2, 3, 5, 6, 7, 8, 9, 0] else draw_dight(False)# 6
    draw_dight(True) if num in[1, 2, 3, 4, 7, 8, 9, 0] else draw_dight(False)# 7
    turtle.right(-180)
    turtle.penup()
    turtle.fd(30)

def draw_date(date):
    turtle.pencolor("red")
    for i in date:
        if i == '-':
            turtle.write('年', font=("Arial", 18, "normal"))
            turtle.pencolor("green")
            turtle.fd(40)
        elif i == '=':
            turtle.write('月', font=("Arial", 18, "normal"))
            turtle.pencolor("blue")
            turtle.fd(40)
        elif i == '+':
            turtle.write('日', font=("Arial", 18, "normal"))
        else:
            draw_sevendight(eval(i))

def main():
    turtle.screensize(1000, 600, "grey")
    turtle.pensize(5)
    turtle.penup()
    turtle.fd(-400)
    turtle.pendown()
    draw_date(datetime.datetime.now().strftime('%Y-%m=%d+'))
    turtle.hideturtle()
    turtle.done()
main()

运行结果:
在这里插入图片描述

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页