day11-学生系统文件存储版

_init_.py 主体入口

from student_manager_system1.dl_t import * # 控制登录注册封包
from student_manager_system1.gl_s import * # 管理学生功能封包

if __name__ == '__main__':
    while True:
        print("="*100)
        print('学生管理系统-教师端'.center(100,' '))
        print('\n\n1.登录\n\n2.注册\n\n3.退出'.center(100,' '))
        print("="*100)
        order = input('输入命令:')
        if order == '1':
            user = input('username:')
            pwd = input('password:')
            if login(user,pwd):
                print('login success')
                while True:
                    print('=' * 100)
                    title_sys = '学生信息管理系统'
                    title_fun1 = '1       添加学生'
                    title_fun2 = '2       查看学生'
                    title_fun3 = '3       删除学生'
                    title_fun4 = '4       立即退出'
                    print(title_sys.center(100), end='\n\n')
                    print(title_fun1.center(100))
                    print(title_fun2.center(100))
                    print(title_fun3.center(100))
                    print(title_fun4.center(100))
                    print('=' * 100)
                    order = input('请输入您的命令:')
                    if order == '1':
                        while True:
                            name = input('输入学生姓名:')
                            age = input('输入学生年龄:')
                            sid = input('输入学生学号:')
                            stu_add(name, age, sid, user)
                            add_ok = input('是否继续添加y/n:')
                            if add_ok == 'n':
                                break
                    elif order == '2':
                        all_info(user)
                    elif order == '3':
                        while True:
                            del_name = input('请输入学生学号:')
                            del_stu(del_name,user)
                            del_ok = input('是否继续删除y/n:')
                            if del_ok == 'n':
                                break
                        pass
                    else:
                        break
            else:
                continue
        elif order == '2':
            print('please input your infomation:')
            user = input('username:')
            pwd = input('password:')
            while True:
                p_c = input('put password again:')
                if p_c != pwd:
                    print('input again error!')
                    continue
                else:
                    break
            regist(user,pwd)
            continue
        elif order == '3':
            break
        else:
            continue

dl_t.py

import json

def login(user,pwd):
    with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
        context = json.load(teacher)
        if {user:pwd} not in context:
            print('please check your username or password!we will return')
            return False
        else:
            print('login in >>>>')
            return True

def regist(user,pwd):
    with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
        context = json.load(teacher)
    context.append({user:pwd})
    with open('./s_m_files/teacher.json','w',encoding='utf-8') as teacher:
        json.dump(context,teacher)
        print('registed success')
        return True

gl_s.py

import json

def stu_add(name,age,sid,teacher):
    student_info = {'name': None, 'sid': None, 'age': None}
    student_info['name'] = name
    student_info['age'] = age
    student_info['sid'] = sid
    try:
        with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as add_s:
            students = json.load(add_s)
    except FileNotFoundError:
        students = []
    with open('./s_m_files/'+teacher+'.json','w', encoding='utf-8') as add_s:
        students.append(student_info)
        json.dump(students,add_s)
    print('添加成功')

def all_info(teacher): # 所有学生信息查询
    try:
        with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as a_info_s:
            students = json.load(a_info_s)
        if len(students) == 0:
            print('没有学生!')
        else:
            for x in students:
                for y in x:
                    print(y + ':' + x[y], end=' ')
                print()
    except FileNotFoundError:
        print('没有学生!')

def del_stu(del_name,teacher): # 学生信息删除
    try:
        with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as d_s:
            students = json.load(d_s)
        if len(students) == 0:
            print('没有学生!')
        elif del_name not in [x['sid'] for x in students]:
            print('没有该学生')
        else:
            for x in students[:]:
                if x['sid'] == del_name:
                    students.remove(x)  # 获取字典在列表中的位置,使用remove删除
            with open('./s_m_files/' + teacher + '.json', 'w', encoding='utf-8') as d_s:
                json.dump(students,d_s)
            print('删除成功')
    except FileNotFoundError:
        print('没有学生!')

实现了教师登录,注册功能,实现添加学生,删除学生,查看所有学生功能,因为修改信息和单个查找的实现类似于添加学生和删除学生,这就没写,主要思想就是登陆方用字典存储{user : pwd},学生用字典外套列表实现存储[{name : xxx , sid : xxxxx, age : xx}],同时为每个登录名分配一个学生列表的json数据文件,当然可以使用一个文件搞定,但是数据显得臃肿庞大,举一个文件的思路最外层使用列表形式[],里面以字典为元素,而这个元素里面存放3个键值对分别是登录者的登录号,密码,和他管理的学生,其中学生项的值用字典外套列表表示。大概样子是[{user:xx , pwd:xxxx , students:[{},{},{}]}],有兴趣的可以一试,就是遍历的弯弯有点多,需要耐心排错。

原谅我不写注释,但是我基本把功能分明了,每一个函数拆开都能看,应该比较好理解。

文件结构:

day11-学生系统文件存储版_第1张图片
image.png

效果图

C:\Users\FL5600\PycharmProjects\venv\Scripts\python.exe C:/Users/FL5600/PycharmProjects/student_manager_system1/__init__.py
====================================================================================================
                                             学生管理系统-教师端                                             
                                         

1.登录

2.注册

3.退出                                         
====================================================================================================
输入命令:2
please input your infomation:
username:ssss
password:12345678
put password again:1234567
input again error!
put password again:12345678
registed success
====================================================================================================
                                             学生管理系统-教师端                                             
                                         

1.登录

2.注册

3.退出                                         
====================================================================================================
输入命令:1
username:ssss
password:12345678
login in >>>>
login success
====================================================================================================
                                              学生信息管理系统                                              

                                            1       添加学生                                            
                                            2       查看学生                                            
                                            3       删除学生                                            
                                            4       立即退出                                            
====================================================================================================
请输入您的命令:1
输入学生姓名:jack
输入学生年龄:23
输入学生学号:2018001
添加成功
是否继续添加y/n:y
输入学生姓名:tony
输入学生年龄:24
输入学生学号:2018002
添加成功
是否继续添加y/n:n
====================================================================================================
                                              学生信息管理系统                                              

                                            1       添加学生                                            
                                            2       查看学生                                            
                                            3       删除学生                                            
                                            4       立即退出                                            
====================================================================================================
请输入您的命令:2
name:jack sid:2018001 age:23 
name:tony sid:2018002 age:24 
====================================================================================================
                                              学生信息管理系统                                              

                                            1       添加学生                                            
                                            2       查看学生                                            
                                            3       删除学生                                            
                                            4       立即退出                                            
====================================================================================================
请输入您的命令:3
请输入学生学号:2018005
没有该学生
是否继续删除y/n:y
请输入学生学号:2018001
删除成功
是否继续删除y/n:n
====================================================================================================
                                              学生信息管理系统                                              

                                            1       添加学生                                            
                                            2       查看学生                                            
                                            3       删除学生                                            
                                            4       立即退出                                            
====================================================================================================
请输入您的命令:2
name:tony sid:2018002 age:24 
====================================================================================================
                                              学生信息管理系统                                              

                                            1       添加学生                                            
                                            2       查看学生                                            
                                            3       删除学生                                            
                                            4       立即退出                                            
====================================================================================================
请输入您的命令:4
====================================================================================================
                                             学生管理系统-教师端                                             
                                         

1.登录

2.注册

3.退出                                         
====================================================================================================
输入命令:1
username:ghjk
password:235678
please check your username or password!we will return
====================================================================================================
                                             学生管理系统-教师端                                             
                                         

1.登录

2.注册

3.退出                                         
====================================================================================================
输入命令:3

Process finished with exit code 0

更新相关:

  • 学号自动生成,在s_m_files中创建一个studnt_count.txt文件存储学生数量,不管哪个老师添加学生都要在生成学号前先读取studnt_count.txt中的数量,在添加学生后,数量+1再写入,关键代码如下:

file_manager.py

import json


def txtfile_read(filename):
    while True:
        try:
            with open(filename,'r',encoding='utf-8') as txtfile:
                content = txtfile.read()
                print('读取成功')
            return content
            break
        except FileNotFoundError:
            order = input('文件未找到,是否创建?y/n:')
            if order == 'y':
                f = open(filename,'w',encoding='utf-8')
                f.close()
                order1 = input('是否写入内容?y/n:')
                if order1 == 'y':
                    new = input('输入写入内容:')
                    with open(filename,'w',encoding='utf-8') as txtfile:
                        txtfile.write(new)
                        print('写入成功!')
            else:
                filename = input('请重新输入路径:')


def txtfile_write(filename,title):
        with open(filename,'w',encoding='utf-8') as txtfile:
            txtfile.write(str(title))
            print('写入成功')

gl_s中的添加学生的方法

from student_manager_system1.file_manager import txtfile_read,txtfile_write

def stu_add(name,age,teacher):
    student_info = {'name': None, 'sid': None, 'age': None}
    student_info['name'] = name
    student_info['age'] = age
    student_count = int(txtfile_read('./s_m_files/student_count.txt')) # 读取已经存在的所有学生数量
    student_counts = student_count + 1 # 添加一个学生就+1(因为生成学号,就是删除学生该值也不会变小)
    student_info['sid'] = '2018'+str(student_counts).rjust(4,'0') # 自动生成
    txtfile_write('./s_m_files/student_count.txt',student_counts) # 覆盖写入
    try:
        with open('./s_m_files/'+teacher+'.json', 'r', encoding='utf-8') as add_s:
            students = json.load(add_s)
    except FileNotFoundError:
        students = []
    with open('./s_m_files/'+teacher+'.json','w', encoding='utf-8') as add_s:
        students.append(student_info)
        json.dump(students,add_s)
    print('添加成功')
  • 注册名重复判断:主要思想是
    context = json.load(teacher)
    {user:pwd}.keys() in [x.keys() for x in context]
    判断输入的键是否存在于已经保存各登录名的键中

dl_t.py

def regist(user,pwd):
    with open('./s_m_files/teacher.json','r',encoding='utf-8') as teacher:
        context = json.load(teacher)
    if {user:pwd}.keys() in [x.keys() for x in context]:
        print('this username is exited!')
        return False
    else:
        context.append({user:pwd})
        with open('./s_m_files/teacher.json','w',encoding='utf-8') as teacher:
            json.dump(context,teacher)
            print('registed success')
            return True

打包exe操作(带清屏功能):

day11-学生系统文件存储版_第2张图片
image.png

day11-学生系统文件存储版_第3张图片
image.png

day11-学生系统文件存储版_第4张图片
image.png

你可能感兴趣的:(day11-学生系统文件存储版)