Python 学生信息管理系统 2.0

本次改进的方面有:
1.通过函数把不同的功能封装成为了一个个模块,使主程序看起来清晰简明。
2.把信息都通过文件操作写入了json文件中,在退出系统后重新登录时数据都还存在。

整个工程分为了四个.py文件,主程序文件为mianmenu.py ,还有三个模块文件分别为 file_operation.py(文件封装模块),land.py(登录模块以及主要功能模块),regis.py(注册模块)

mianmenu.py

# @Author  : Fizzyi
import regist
import land

def mainmenu():
    #主界面
    while True:
        print(''''
            ---- 欢迎 ----
            1.登陆
            2.注册
            3.退出
        ''')
        order = input('请输入:')
        if order == '1':
            ##进入登录界面
            land.landing()
        elif order == '2':
            #进入注册界面
            regist.regist_menu()
        else:
            #退出程序
            break
if __name__ == '__main__':
    stu_id = 0
    mainmenu()

模块 1file_operation.py

# @Author  : Fizzyi
import json

def basic_file_read(file):
    #普通文本文件的读取
    with open(file,'r',encoding='utf-8') as f:
        return f.read()

def basic_file_write(file,content):
    #普通文本文件的写
    content = json.dumps(content)
    with open(file,'w',encoding='utf-8') as f:
        f.write(content)

模块2 land.py

# @Author  : Fizzyi
import file_operation as files
import json

def examine_name_pwd():
    #验证账号密码是否正确
    user_name = input('输入账号:')
    user_psw = input('请输入密码:')
    file = 'userinfo/'+user_name+'.json'
    dict1 = {}
    try:
        dict1 = json.loads(files.basic_file_read(file)) #通过loads将json格式的文件转换成python格式的字典
    except:
         print('该账号未注册')
         return
    if dict1[user_name] == user_psw:
             print('密码输入正确')
    else:
        print('密码输入错误')
        return
    return file

def landing_menu(file):
    #学生信息关系系统主界面
    while True:
        print('''
            1.查看学生信息
            2.添加学生信息
            3.修改学生信息
            4.删除学生信息
            5.退出
        ''')

        order = input('请输入-->')
        if order == '1':
            ##查看学生信心
            showall(file)
        elif order == '2':
            #添加学生信息
            addstu(file)
        elif order == '3':
            #修改学生信息
            exchange(file)
        elif order == '4':
            #删除学生信息
            delstu(file)
        else:
            #退出
            break

def showall(file):
    #展示所有学生信息的函数
    all_content = json.loads(files.basic_file_read(file))
    for key in all_content['student'].keys():
        print('姓名:%s,年龄:%s,性别:%s'%(key,all_content['student'][key]['age'],all_content['student'][key]['sex']))


def addstu(file):
    #添加学生信息的函数
    new_stu_name = input('请输入学生姓名:')
    new_stu_age = input('请输入学生年龄:')
    new_stu_sex = input('请输入学生性别:')
    with open(file,'r',encoding='utf-8') as addstus:
        content = json.load(addstus)
        content['student'][new_stu_name]={'age':new_stu_age,'sex':new_stu_sex}
    with open(file,'w',encoding='utf-8') as f:
        json.dump(content,f)
    print('添加成功')

def exchange(file):
    #修改学生信息的函数 先不考虑重名的情况
    find_name = input('请输入要修改的学生姓名:')
    with open(file,'r',encoding='utf-8') as allstus:
        content =json.load(allstus)
        for key  in content['student'].keys():
            if key == find_name :
                #如果找到这位同学
                finde_key=input('请输入要修改的类型(age,sex):')
                finde_content = input('请输入修改的内容:')
                content['student'][key][finde_key] = finde_content
                print('修改成功')
                break
        else:
            print('未找到这名同学的信息')
    with open(file,'w',encoding='utf-8') as allstus:
        json.dump(content,allstus)

def delstu(file):
    #删除学生信息的模块
    delstu_name = input('请输入要删除的学生的姓名:')
    with open(file,'r',encoding='utf-8') as allstus:
        content =json.load(allstus)
        for key  in content['student'].keys():
            if key == delstu_name :
                del content['student'][key]
                print('删除成功')
                break
        else:
            print('未找到这名同学的信息')
    with open(file,'w',encoding='utf-8') as allstus:
        json.dump(content,allstus)
def landing():
    #登录后先执行账号密码验证函数 在执行主函数
    file = examine_name_pwd()
    landing_menu(file)

模块3 regis.py

# @Author  : Fizzyi

#注册功能的实现


import file_operation as files
#导入文件操作模块

def regist_menu():
    user_name = input('请输入用户名:')
    user_pwd = input('请输入密码:')
    user_repwd = input('请再次输入密码:')
    if user_pwd != user_repwd:
        #验证两次输入的密码是否相等
        print('输入的两次密码不相等,请重新注册')
        return
    file = 'userinfo/'+user_name+'.json' #将用户名设为json文件的文件名
    try:                                  #判断用户名是否存在
        with open(file,'r',encoding='utf-8'):
            print('该账号已被注册,请重新注册')
    except:
        content ={user_name:user_pwd,'student':{}} # 将用户名和密码以键值对的方式写入文件
        files.basic_file_write(file,content)  #调用文件写入函数
        print('注册成功')

在每次注册成功后,会在工程的userinfo文件夹下新建一个以用户名为名字的.json文件,文件的初始内容为两个键值对,第一个kye和value为用户名和密码


1.png
2.png
3.png

完整运行程序过程图如下:


complete_operation_process.png

json文件内容如下:

{"zhy": "123", "student": {"asdf": {"age": "20", "sex": "\u5973"}}}

你可能感兴趣的:(Python 学生信息管理系统 2.0)