Python實現七段數碼管的日期顯示
阿新 • • 發佈:2018-11-11
顯示要求:
i代'd碼實u現:
#引入turtle庫 import turtle as tu #引入time庫 import time #定義繪製數碼管間隔函式,即每段之間要有間隔 def drawGap(): tu.penup() tu.fd(10) #定義畫線函式 def drawLine(draw): drawGap() #定義每段數碼管的間隔 tu.pendown() if draw else tu.penup() #控制畫筆起落 tu.fd(40) drawGap() tu.right(90) #向右轉90度 #定義數碼管函式 def drawDigit(digit): drawLine(True) if digit in [2, 3, 4, 5, 6, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 1, 3, 4, 5, 6, 7, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 2, 3, 5, 6, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 2, 6, 8] else drawLine(False) #此時回到了起點 tu.left(90) drawLine(True) if digit in [0, 4, 5, 6, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 2, 3, 5, 6, 7, 8, 9] else drawLine(False) drawLine(True) if digit in [0, 1, 2, 3, 4, 7, 8, 9] else drawLine(False) tu.left(180) tu.penup() tu.fd(20) #定義所畫資料的函式 def drawDate(date): #data為日期,格式為'%Y-%m=%d+' tu.pencolor("red") for i in date: if i == "-": tu.write('年', font=("Arial", 18, "normal")) #格式規範 tu.pencolor("green") tu.fd(40) elif i == '=': tu.write('月', font=("Arial", 18, "normal")) tu.pencolor("blue") tu.fd(40) elif i == '+': tu.write('日', font=("Arial", 18, "normal")) else: drawDigit(eval(i)) #定義主函式呼叫這寫函式 def main(): tu.setup(1000, 350, 200, 200) tu.penup() tu.fd(-300) tu.pensize(5) drawDate(time.strftime("%Y-%m=%d+", time.gmtime())) #time中的strftime函式對時間進行格式化 tu.hideturtle() tu.done() #執行main函式 main()