这是一个简单的考勤小程序的 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:
            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字典中。程序运行后,输入迟到同学的学号,程序会根据学号判断是否在考勤表中,如果有该学号,则将其迟到次数加1;如果没有该学号,则显示提示信息。输入'exit'或'quit'可以停止程序,并显示统计信息。

统计信息按照迟到次数降序、升序以及原次序分别显示,并输出学号、姓名和迟到次数。

请根据你的实际需求和学号名单进行相应的修改。


原文地址: https://www.cveoy.top/t/topic/pSx 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录