编制python程序绘制七段数码管:输入当前日期的数字形式,输出数码管表示的图形形式
时间: 2023-06-01 20:04:44 浏览: 119
好的,我可以帮你回答这个问题。以下是 Python 代码示例:
```
import turtle
import time
def drawLine(draw):
if draw:
turtle.pendown()
else:
turtle.penup()
turtle.fd(40)
turtle.right(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)
turtle.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)
turtle.left(180)
turtle.penup()
turtle.fd(20)
def drawDate(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:
drawDigit(eval(i))
def main():
turtle.setup(800, 350, 200, 200)
turtle.penup()
turtle.fd(-300)
turtle.pensize(5)
drawDate(time.strftime("%Y-%m=%d+", time.gmtime()))
turtle.hideturtle()
turtle.done()
main()
```
这个程序通过 Python 中的 turtle 库来实现七段数码管的绘制。你可以将当前的日期以字符串形式输入,如 '2021-05-01',然后程序将输出对应的七段数码管图形。希望这能够帮到你。
阅读全文