Backend Development 8 min read

Python-Based Student Attendance System – Project Description and Implementation Guide

This article outlines a Python-driven electronic attendance system project, detailing required CSV data structures, core functions for loading student information, user login, attendance recording, and query features, along with code snippets and step-by-step instructions for students to complete and test the application.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python-Based Student Attendance System – Project Description and Implementation Guide

Project Overview : The school requires an electronic attendance system. Since the class has learned Python for big‑data development, students are asked to implement the student‑side attendance functionality.

Requirements :

(1) The system already has partial functionality; remaining features are marked with #todo comments.

(2) Student information is stored in stu_infos.csv (header row followed by rows containing student number, name, and password).

(3) Attendance records are saved to attendance.csv (header row followed by rows containing student number, name, timestamp, and status – one of "出勤", "迟到", "请假", "缺勤").

(4) Student data must be loaded into a list named student_infos , where each element is a dictionary mapping column names to values.

(5) The teacher side consists of two Python files: main.py (entry point) and stu_attendance.py (functions for data loading, login, etc.).

Task Requirements :

Add a row with your own information to stu_infos.csv (your real name and student number, password can be arbitrary).

Inspect the #todo comments in the two Python files, write the missing code, and provide the added code snippets.

Test the program: use your student number for login, and verify two scenarios – three consecutive login failures and a successful login followed by a successful attendance entry. Provide screenshots of the program output.

Additional Feature : Implement a query function that, given a student's name, retrieves and displays all of their attendance records.

Code Snippets :

<code>import csv
import time
student_infos = []</code>
<code>def load_stu_info():
    """加载学生信息
    从stu_infos.csv文件中加载数据
    :return: 无"""
    with open(r"stu_infos.csv", encoding='utf-8-sig') as file:
        f_csv = csv.reader(file)
        header = next(f_csv)
        for row in f_csv:
            student_info = {}
            for index in range(3):
                student_info[header[index]] = row[index]
            student_infos.append(student_info)
</code>
<code>def login():
    """用户使用学号和密码进行登录
    最多让用户登录三次,如果连续三次都登录失败(用户名或者密码错误),只要密码和用户都正确表示登录成功
    :return:登录成功返回True和学号,三次都登录失败返回False和None"""
    retry_time = 0
    while retry_time < 3:
        user_no = input('请输入登录账号:')
        password = input('请输入密码:')
        for i in student_infos:
            if i['no'] == user_no and i['password'] == password:
                return True, user_no
        print('用户名或者密码错误!!!请重新输入。')
        retry_time += 1
    else:
        return False, None
</code>
<code>def add(user_no):
    for x in student_infos:
        if user_no == x['no']:
            name = x['name']
            break
    times = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    choices = ['出勤', '迟到', '请假', '缺勤']
    a = int(input("\t该学生出勤情况:1-出勤\t2-迟到\t3-请假\t4-缺勤:"))
    if a == 1:
        data = choices[0]
    elif a == 2:
        data = choices[1]
    elif a == 3:
        data = choices[2]
    else:
        data = choices[3]
    with open(r"attendance.csv", 'a+', newline='', encoding='utf-8') as f:
        wf = csv.writer(f)
        wf.writerow([user_no, name, times, data])  # 写入一行数据
        print("{}同学{}数据已经写入成功!操作时间是{}".format(name, data, times))
</code>
<code>def select():
    student = []
    with open(r"attendance.csv", encoding='utf-8-sig') as file:
        f_csv = csv.reader(file)
        header = next(f_csv)
        for row in f_csv:
            students = {}
            for index in range(4):
                students[header[index]] = row[index]
            student.append(students)
    name = input("请输入你需要查找的姓名:")
    print("  学号\t\t姓名\t\t操作时间\t\t出勤状态")
    for a in student:
        if a['name'] == name:
            print(a['no'] + '\t' + a['name'] + '\t' + a['time'] + '\t\t' + a['state'])
        else:
            print("无此人!!!")
            break
</code>
<code>from student.stu_attendance import *
if __name__ == '__main__':
    load_stu_info()
    success, stu_no = login()
    print(stu_no)
    if success:
        print('登录成功!')
        add(stu_no)
        q = int(input("你想要查询出勤数据吗?\tyes(1)--no(0)"))
        if q == 1:
            select()
        else:
            print("欢迎下次登录电子考勤系统")
    else:
        print('登录失败')
</code>

The article concludes with screenshots of the program’s execution and promotional material for a free Python public course, which is not part of the academic content.

CLICSVfile-ioAttendancestudent-management
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.