[Python] student management system

Design ideas:

Create a global variable students list to store all student information

The information of each student is saved in the temporary variable stu dictionary when input, and then the dictionary is stored in the students list

Load data from files when entering the system, and write stu data into students

Save the data when exiting the system, save the data in students into the file with each dictionary as a line

Find and delete student information Support fuzzy query

By default, the stored files are stored in the current directory

import sys
import os

students = []

def save():
    with open('students.txt', 'w', encoding='utf-8') as f:
        for s in students:
            f.write(f"{s['stuID']}\t{s['name']}\t{s['gender']}\n")
        print("存档成功")

def load():
    if not os.path.exists('students.txt'):
        return
    global students
    students = []
    with open('students.txt', 'r', encoding='utf-8') as f:
        for line in f:
            line = line.strip()     # strip 可以去掉一个字符串首位的空白符
            tokens = line.split('\t')   # 按照 \t 进行切分
            if len(tokens) != 3:
                print(f"当前格式存在问题,line={line}")
                continue
            stu = {
                'stuID': tokens[0],
                'name': tokens[1],
                'gender': tokens[2]
            }
            students.append(stu)
    print(f"读档成功,共读取了{len(students)}条数据")

def insert():
    stuID = input('请输入学生ID:')
    name = input('请输入学生姓名:')
    gender = input('请输入学生性别:')
    if gender != '男' and gender != '女':     #支持模糊查询
        print('输入性别有误,请重新输入')
        return
    stu = {
        'stuID': stuID,
        'name': name,
        'gender': gender
    }
    global students
    students.append(stu)
    print('新增学生完毕')

def show():
    for s in students:
        print(f"{s['stuID']}\t{s['name']}\t{s['gender']}")
    print(f'显示学生完毕,共显示了{len(students)}条学生数据')

def find():
    # 按学生ID或姓名进行查询
    find = input('请输入要查询学生的学号或姓名')
    count = 0
    for s in students:
        if find in s['stuID'] or find in s['name']:
            print(f"{s['stuID']}\t{s['name']}\t{s['gender']}")
            count += 1
    print(f"查询学生结束,共查询到{count}条学生信息")

def delete():
    delete = input('请输入要删除信息的学生学号或姓名:')
    for s in students:
        if delete in s['stuID'] or delete in s['name']:
            print(f"删除{s['name']}同学的信息")
            students.remove(s)
            break
    print("删除学生信息结束")

def menu():
    print('1. 新增学生')
    print('2. 显示学生')
    print('3. 查找学生')
    print('4. 删除学生')
    print('0. exit')
    choice = input('请输入选项:')
    return choice

if __name__ == '__main__':
    load()
    while True:
        choice = menu()
        if choice == '1':
            insert()
        elif choice == '2':
            show()
        elif choice == '3':
            find()
        elif choice == '4':
            delete()
        elif choice == '0':
            save()
            sys.exit(0)
        else:
            print('选择错误,请重新选择')

Guess you like

Origin blog.csdn.net/phoenixFlyzzz/article/details/129760961
Recommended