怎么修改利用turtle库绘制七段数码管,显示当前日期及学生基本信息,包括学号、姓名?
感觉我的输出结果并不理想, 求指教。
import turtle
import random
import datetime
# 生成验证码
def generate_code(length):
all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
code = ''
for i in range(length):
index = random.randint(0, len(all_chars)-1)
code += all_chars[index]
return code
# 写入学生信息到文件
def write_student_info_to_file(student_info):
with open('student_info.txt', 'w') as f:
f.write(student_info)
# 从文件中读取学生信息
def read_student_info_from_file():
with open('student_info.txt', 'r') as f:
student_info = f.read()
return student_info
# 绘制七段数码管并显示当前日期和学生基本信息
def draw_digit(digit, pen):
if digit in [2, 3, 4, 5, 6, 8, 9]:
pen.pendown()
else:
pen.penup()
if digit in [0, 1, 3, 4, 7, 8, 9]:
pen.forward(50)
else:
pen.penup()
pen.forward(50)
pen.pendown()
pen.right(90)
if digit in [0, 2, 3, 5, 6, 7, 8, 9]:
pen.forward(100)
else:
pen.penup()
pen.forward(100)
pen.pendown()
pen.right(90)
if digit in [0, 2, 6, 8]:
pen.forward(50)
else:
pen.penup()
pen.forward(50)
pen.pendown()
pen.right(90)
if digit in [0, 4, 5, 6, 8, 9]:
pen.forward(100)
else:
pen.penup()
pen.forward(100)
pen.pendown()
pen.right(90)
if digit in [0, 2, 3, 5, 6, 8, 9]:
pen.forward(50)
else:
pen.penup()
pen.forward(50)
pen.pendown()
pen.right(90)
if digit in [0, 1, 2, 3, 4, 7, 8, 9]:
pen.forward(100)
else:
pen.penup()
pen.forward(100)
pen.pendown()
pen.right(90)
if digit in [0, 2, 3, 5, 6, 8, 9]:
pen.forward(50)
else:
pen.penup()
pen.forward(50)
pen.pendown()
def draw_date(pen):
date = datetime.datetime.now().strftime("%Y-%m-%d")
pen.penup()
pen.goto(-150, 150)
pen.pendown()
pen.write(date, font=("Arial", 18, "normal"))
def draw_student_info(pen):
student_info = read_student_info_from_file()
student_info_list = student_info.split(',')
student_id = student_info_list[0]
student_name = student_info_list[1]
pen.penup()
pen.goto(-150, 120)
pen.pendown()
pen.write("学号: " + student_id, font=("Arial", 16, "normal"))
pen.penup()
pen.goto(-150, 90)
pen.pendown()
pen.write("姓名: " + student_name, font=("Arial", 16, "normal"))
def draw_clock():
window = turtle.Screen()
window.bgcolor("white")
window.title("七段数码管")
pen = turtle.Turtle()
pen.speed(0)
pen.hideturtle()
draw_date(pen)
draw_student_info(pen)
pen.penup()
pen.goto(-150, 0)
pen.pendown()
hour = datetime.datetime.now().strftime("%H")
hour = hour if len(hour) == 2 else '0' + hour
minute = datetime.datetime.now().strftime("%M")
second = datetime.datetime.now().strftime("%S")
for i in range(2):
draw_digit(int(hour[i]), pen)
pen.penup()
pen.goto(pen.xcor() + 25, pen.ycor())
pen.pendown()
for i in range(2):
draw_digit(int(minute[i]), pen)
pen.penup()
pen.goto(pen.xcor() + 25, pen.ycor())
pen.pendown()
for i in range(2):
draw_digit(int(second[i]), pen)
pen.penup()
pen.goto(pen.xcor() + 25, pen.ycor())
pen.pendown()
turtle.done()
# 主程序函数
def main():
while True:
print("请选择要执行的功能:")
print("1. 生成验证码")
print("2. 写入学生信息到文件")
print("3. 绘制七段数码管并显示当前日期和学生基本信息")
print("4. 修改学生信息并重新绘制七段数码管")
print("5. 退出程序")
choice = input("请输入要执行的功能编号:")
if choice == '1':
length = int(input("请输入验证码的长度:"))
code = generate_code(length)
print("生成的验证码为:", code)
elif choice == '2':
student_id = input("请输入学号:")
student_name = input("请输入姓名:")
student_info = student_id + ',' + student_name
write_student_info_to_file(student_info)
print("学生信息已写入文件中。")
elif choice == '3':
draw_clock()
elif choice == '4':
student_id = input("请输入新的学号:")
student_name = input("请输入新的姓名:")
student_info = student_id + ',' + student_name
write_student_info_to_file(student_info)
print("学生信息已修改。")
draw_clock()
elif choice == '5':
print("程序已退出。")
break
else:
print("输入有误,请重新输入。")
if __name__ == '__main__':
main()
你是不是不知道什么是7段数码管,先百度图片里搜一下看看到底长什么样子,再代码绘制它