day10作业stu_sys_2_0_1

login界面

import file_funcs import stu_sys# 登录注册界面 def list_login(): print(''' +++++++++++++++++++++++++++++++++++++++++++++++++ +welcome to student manage system+ +1.register+ ++ +2.login+ ++ +0.exit+ +++++++++++++++++++++++++++++++++++++++++++++++++ ''') cmd = input('>>>>>>>') if '0' <= cmd <= '2': return cmd else: print('输入内存错误!') return Falsedef login(): while True: result = list_login() if not result: continue if result == '1': user_name = input('请输入用户名:') users = file_funcs.get_infos('users') for index in range(len(users)): if user_name == users[index]['name']: print('该用户名已经存在!') break else: password = input('请输入密码:') password_confirm = input('请再次输入密码:') if password == password_confirm: new_user = {'name': user_name, 'password': password, 'database': user_name+'_database', 'stu_nums': '0', 'stu_id_next': '1'} file_funcs.add_info('users', new_user) print('注册成功!') else: print('两次密码不一致!注册失败!') if result == '2': user_name = input('请输入用户名:') password = input('请输入密码:') users = file_funcs.get_infos('users') for index in range(len(users)): if user_name == users[index]['name']: if password == users[index]['password']: print('登录成功!') stu_sys.main(user_name, users[index]['database']) else: print('密码错误!') break else: print('用户名不存在!')if result == '0': returnif __name__ == '__main__': login()

文件读写
import json# 获取信息 def get_infos(file_name): try: with open('./files/'+file_name+'.json', 'r', encoding='utf-8') as f: all_stu_infos = json.load(f) except FileNotFoundError: all_stu_infos = []return all_stu_infos# 添加信息 def add_info(file_name, stu_info): # 获取之前的学生信息 all_stu_infos = get_infos(file_name) with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f: # 将新的学生信息添加到所有学生信息的最后 all_stu_infos.append(stu_info) # 保存所有信息 json.dump(all_stu_infos, f) # 返回保存成功的信息 return True# 重新写入 def rewrite(file_name, stu_info): with open('./files/'+file_name+'.json', 'w', encoding='utf-8') as f: # 保存所有信息 json.dump(stu_info, f) # 返回保存成功的信息 return Trueif __name__ == '__main__': pass

dao方法
import file_funcs# 查找学生 def stu_find(file_name): stu_infos = file_funcs.get_infos(file_name) stu_info = input('请输入学生的学号:') for index_info in range(len(stu_infos)): # 学生信息存在 if stu_info == stu_infos[index_info]['stu_id']: return stu_infos[index_info] # 学生信息不存在 else: return stu_info# 查找全部 def stu_find_all(file_name): stu_infos = file_funcs.get_infos(file_name) for index_info in range(len(stu_infos)): print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % (stu_infos[index_info]['name'], stu_infos[index_info]['stu_id'], stu_infos[index_info]['score']['English'], stu_infos[index_info]['score']['PE'], stu_infos[index_info]['score']['art'], stu_infos[index_info]['score']['math'], stu_infos[index_info]['age']))# 添加学生信息 def stu_create(file_name, name, stu_id, english, pe, art, math, age): # stu_infos.append({'name':name, 'stu_id':stu_id,'score':{'English':english, 'PE':pe, 'art':art, 'math':math}, #'age':age}) file_funcs.add_info(file_name, {'name': name, 'stu_id': stu_id, 'score': {'English': english, 'PE': pe, 'art': art, 'math': math}, 'age': age})# 删除学生信息 def stu_del(file_name, stu_info): stu_infos = file_funcs.get_infos(file_name) index_stu = stu_infos.index(stu_info) del stu_infos[index_stu] file_funcs.rewrite(file_name, stu_infos) print('删除信息成功!')def stu_num_id_get(user_name): users = file_funcs.get_infos('users') for index in range(len(users)): if user_name == users[index]['name']: stu_nums = users[index]['stu_nums'] stu_id = users[index]['stu_id_next'] users[index]['stu_nums'] = str(int(users[index]['stu_nums'])+1) users[index]['stu_id_next'] = str(int(users[index]['stu_id_next'])+1) file_funcs.rewrite('users', users) return stu_nums, stu_iddef stu_num_del(user_name): users = file_funcs.get_infos('users') for index in range(len(users)): if user_name == users[index]['name']: users[index]['stu_nums'] = str(int(users[index]['stu_nums']) - 1) file_funcs.rewrite('users', users) returnif __name__ == '__main__': pass

学生管理系统
import daodef list_main(): print(''' +++++++++++++++++++++++++++++++++++++++++++++++++ +welcome to student manage system+ +1.add student information+ +2.View personal information by id+ +3.del personal information by id+ +4.view all information+ +5.view personal average score by id+ +0.exit+ +++++++++++++++++++++++++++++++++++++++++++++++++ ''') user_cmd = input('>>>>>') return user_cmd# 判断用户输入 def user_input(user_cmd): if user_cmd == '1': while True: stu_nums, stu_id = dao.stu_num_id_get(file_user) name, english, pe, art, math, age = stu_info_input() dao.stu_create(file_name, name, stu_id, english, pe, art, math, age) # 判断是否继续或者退出 print(''' 1.继续添加 2.返回上一层 ''') user_cmd = input('>>>>>') if user_cmd == '1': continue elif user_cmd == '2': return else: print('输入有误!') returnelif user_cmd == '2': while True: stu_info = dao.stu_find(file_name) if isinstance(stu_info, str): print('学生信息不存在!') else: # 打印学生信息 print('姓名:%s\t学号:%s\t英语:%s\t体育:%s\t美术:%s\t数学:%s\t年龄:%s\t' % ( stu_info['name'], stu_info['stu_id'], stu_info['score']['English'], stu_info['score']['PE'], stu_info['score']['art'], stu_info['score']['math'], stu_info['age'])) # 判断是否继续或者退出 print(''' 1.继续查看 2.返回上一层 ''') user_cmd = input('>>>>>') if user_cmd == '1': continue elif user_cmd == '2': return else: print('输入有误!') returnelif user_cmd == '3': while True: stu_info = dao.stu_find(file_name) if isinstance(stu_info, str): print('学生信息不存在!') else: dao.stu_del(file_name, stu_info) dao.stu_num_del(file_user) # 判断是否继续或者退出 print(''' 1.继续删除 2.返回上一层 ''') user_cmd = input('>>>>>') if user_cmd == '1': continue elif user_cmd == '2': return else: print('输入有误!') returnelif user_cmd == '4': while True: dao.stu_find_all(file_name) # 判断是否继续或者退出 print(''' 1.继续查看 2.返回上一层 ''') user_cmd = input('>>>>>') if user_cmd == '1': continue elif user_cmd == '2': return else: print('输入有误!') returnelif user_cmd == '5': while True: stu_info = dao.stu_find(file_name) if isinstance(stu_info, str): print('学生信息不存在!') else: score_ave(stu_info) # 判断是否继续或者退出 print(''' 1.继续查询 2.返回上一层 ''') user_cmd = input('>>>>>') if user_cmd == '1': continue elif user_cmd == '2': return else: print('输入有误!') returnelif user_cmd == '0': exit(0) else: print('输入内容有误!')# 用户输入学生信息 def stu_info_input(): name = input('请输入学生的姓名:') english = input('请输入音乐成绩:') pe = input('请输入体育成绩:') art = input('请输入美术成绩:') math = input('请输入数学成绩:') age = input('请输入学生的年龄:') return name, english, pe, art, math, age# 平均成绩 def score_ave(stu_info): score_average = (int(stu_info['score']['English']) + int(stu_info['score']['PE']) + int(stu_info['score']['art']) + int(stu_info['score']['math'])) / 4 print('姓名:%s\t学号:%s\t平均成绩是:%0.2f' % ( stu_info['name'], stu_info['stu_id'], score_average))# 主程序 def main(user_name, user_database): global file_user file_user = user_name global file_name file_name = user_database # global stu_infos # stu_infos = file_funcs.get_infos(user_name) while True: # 打印列表内容,等待用户输入 user_input(list_main())# 开始程序 if __name__ == '__main__': pass

    推荐阅读