Python 考勤程序:自动添加学号并记录迟到次数
Python 考勤程序:自动添加学号并记录迟到次数
该程序实现了一个简单的考勤系统,支持手动输入学号记录迟到次数。当输入的学号不在考勤库内时,会询问是否添加该学号,并进行考勤记录。
def attendance_program():
student_attendance = {}
student_list = {
'0000001': '张三',
'0000002': '李四',
'0000003': '王五'
}
while True:
student_id = input('请输入迟到同学的学号:')
if student_id.lower() == 'exit' or student_id.lower() == 'quit':
break
if student_id in student_list:
if student_id in student_attendance:
student_attendance[student_id] += 1
else:
student_attendance[student_id] = 1
else:
add_new_student = input('该学号不在考勤表中,是否添加该学号?(是/否)')
if add_new_student.lower() == '是':
student_name = input('请输入该学生的姓名:')
student_list[student_id] = student_name
student_attendance[student_id] = 1
print('学号已添加并进行考勤记录')
else:
print('不添加学号,不进行考勤记录')
print('统计信息:')
print('按迟到次数降序:')
sorted_attendance = sorted(student_attendance.items(), key=lambda x: x[1], reverse=True)
for student_id, count in sorted_attendance:
print(f'学号:{student_id},姓名:{student_list[student_id]},迟到次数:{count}')
print('按迟到次数升序:')
sorted_attendance = sorted(student_attendance.items(), key=lambda x: x[1])
for student_id, count in sorted_attendance:
print(f'学号:{student_id},姓名:{student_list[student_id]},迟到次数:{count}')
print('按原次序:')
for student_id in student_list:
if student_id in student_attendance:
count = student_attendance[student_id]
else:
count = 0
print(f'学号:{student_id},姓名:{student_list[student_id]},迟到次数:{count}')
attendance_program()
在代码中,当输入的学号不在考勤库内时,会询问是否添加该学号。如果回答是,则需要输入该学生的姓名,并将学号和姓名添加到学生名单(student_list)中,并进行考勤记录。如果回答否,则不添加学号,并不进行考勤记录。
请根据你的实际需求和学号名单进行相应的修改。
原文地址: https://www.cveoy.top/t/topic/pTP 著作权归作者所有。请勿转载和采集!