赞
踩
总共四个代码文件如下:
文件操作(file_manger)
学生界面(student)
进行数据加密(tools)
主界面(index)
具体文件请看文末
register 账号密码规范函数
- def register():
- global teacher
- while True:
- teacher_name = input('请输入账号(3-6位):')
- if len(teacher_name) > 6 or len(teacher_name) < 3:
- print('账号不符合要求,请重新输入')
- else:
- break
- while True:
- password = input('请输入密码(6-12位)')
- if not 6 <= len(password) <= 12:
- print('密码不符合要求,请重新输入')
- else:
- break
- # 用户名密码账号密码都输入正确
- password = tools.encrypt_password(password)
- teacher[teacher_name] = password
- file_manger.write_json('data.json', teacher)
-
login学生登陆函数:
-
- def login():
- m = file_manger.load_json('data.json')
- n = input('请输入需要登入的账号:')
- for i in m.keys():
- if n == i:
- y = input('请输入密码')
- y = tools.encrypt_password(y)
- if y == m[i]:
- print('登陆成功')
- while True:
- showmenu()
- x = input('请选择你需要操作的序号:')
- if x == '1':
- inserts()
- elif x == '2':
- removestu()
- elif x == '3':
- modstu()
- elif x == '4':
- chaxun()
- elif x == '5':
- showstu()
- elif x == '6':
- bao()
- elif x == '7':
- print('6.退出系统')
- print('欢迎下次使用该系统')
- break
- else:
- print('输入序号有误,请重新输入')
- continue
- input('....回车键继续操作....')
- sys.exit(0)
- else:
- print('密码输入错误')
- break
- else:
- print('账号输入错误')
start主界面函数:
-
- def start():
- content = file_manger.read_files('welcome.txt')
- while True:
- operator = input(content + '\n请选择(1-3):')
- if operator == '1':
- print('登陆')
- login()
- elif operator == '2':
- print('注册')
- register()
- elif operator == '3':
- print('退出')
- # break 退出死循环
- # exit(0) 退出整个程序
- sys.exit(0)
- else:
- print('输入有误')
- input('请输入任意字符继续')
index.py
-
- import file_manger
-
- import tools
-
- from student import *
-
- teacher = {}
-
-
- def register():
- global teacher
- while True:
- teacher_name = input('请输入账号(3-6位):')
- if len(teacher_name) > 6 or len(teacher_name) < 3:
- print('账号不符合要求,请重新输入')
- else:
- break
- while True:
- password = input('请输入密码(6-12位)')
- if not 6 <= len(password) <= 12:
- print('密码不符合要求,请重新输入')
- else:
- break
- # 用户名密码账号密码都输入正确
- password = tools.encrypt_password(password)
- teacher[teacher_name] = password
- file_manger.write_json('data.json', teacher)
-
-
- def login():
- m = file_manger.load_json('data.json')
- n = input('请输入需要登入的账号:')
- for i in m.keys():
- if n == i:
- y = input('请输入密码')
- y = tools.encrypt_password(y)
- if y == m[i]:
- print('登陆成功')
- while True:
- showmenu()
- x = input('请选择你需要操作的序号:')
- if x == '1':
- inserts()
- elif x == '2':
- removestu()
- elif x == '3':
- modstu()
- elif x == '4':
- chaxun()
- elif x == '5':
- showstu()
- elif x == '6':
- bao()
- elif x == '7':
- print('6.退出系统')
- print('欢迎下次使用该系统')
- break
- else:
- print('输入序号有误,请重新输入')
- continue
- input('....回车键继续操作....')
- sys.exit(0)
- else:
- print('密码输入错误')
- break
- else:
- print('账号输入错误')
-
-
- def start():
- content = file_manger.read_files('welcome.txt')
- while True:
- operator = input(content + '\n请选择(1-3):')
- if operator == '1':
- print('登陆')
- login()
- elif operator == '2':
- print('注册')
- register()
- elif operator == '3':
- print('退出')
- # break 退出死循环
- # exit(0) 退出整个程序
- sys.exit(0)
- else:
- print('输入有误')
- input('请输入任意字符继续')
-
-
- if __name__ == '__main__':
- start()
file_manger.py
- import json
-
-
- def read_files(file_name):
- try:
- with open(file_name, 'r', encoding='utf-8') as f:
- content = f.read()
- return content
- except FileExistsError:
- print('文件未找到')
-
-
- def write_json(file_name, data):
- with open(file_name, 'w', encoding='utf-8') as f:
- json.dump(data, f)
-
-
- def load_json(file_name):
- with open(file_name, 'r', encoding='utf-8') as f:
- return json.load(f)
tools.py
- import hashlib
-
-
- # 进行加密
- def encrypt_password(x):
- sha256 = hashlib.sha256(x.encode('utf-8'))
- x = sha256.hexdigest()
- return x
student.py
- import json
- import sys
-
- stulist = []
-
-
- def showmenu():
- print('♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥')
- print('---------------学生成绩管理系统--------------')
- print('---------------1.添加学生--------------------')
- print('---------------2.删除学生--------------------')
- print('---------------3.修改单个学生信息------------')
- print('---------------4.查询单个学生信息------------')
- print('---------------5.查询所有的学生信息----------')
- print('---------------6.保存所有的学生信息----------')
- print('---------------7.退出系统--------------------')
- print('♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥♥')
-
-
- def inserts(): # 添加学生信息
- name = input('请输入学生姓名:')
- for stu in stulist:
- if stu['name'] == name:
- print('学生信息存在')
- return
- age = input('请输入学生年龄:')
- sex = input('请输入学生性别:')
- stu = {'name': name, 'age': int(age), 'sex': sex}
- stulist.append(stu)
- print('学生信息添加完成')
-
-
- def showstu(): # 查询所有学生信息
- if len(stulist):
- for stu in stulist:
- print(f"性别:{stu['name']} 年龄:{stu['age']} 性别:{stu['sex']}")
- else:
- print('目前无学生信息')
-
-
- def removestu(): # 删除学生信息
- name = input('请输入学生姓名:')
- for stu in stulist:
- if stu['name'] == name:
- stulist.remove(stu)
- break
- else:
- print('学生信息不存在,无法删除')
-
-
- def modstu(): # 更改学生年龄
- name = input('请输入学生姓名:')
- for stu in stulist:
- if stu['name'] == name:
- stu['age'] = int(input('请输入新的年龄:'))
- break
- else:
- print('学生信息不存在,无法修改')
-
-
- def chaxun(): # 查看个人学生信息
- name = input('请输入需要查询的名字')
- for stu in stulist:
- if stu['name'] == name:
- print(f"性别:{stu['name']} 年龄:{stu['age']} 性别:{stu['sex']}")
- break
- else:
- print('目前无学生信息')
-
-
- def bao():
- with open(r'studentxinxi.json', 'w') as f:
- json.dump(stulist, f)
-
-
- if __name__ == '__main__':
- while True:
- showmenu()
- x = input('请选择你需要操作的序号:')
- if x == '1':
- inserts()
- elif x == '2':
- removestu()
- elif x == '3':
- modstu()
- elif x == '4':
- chaxun()
- elif x == '5':
- showstu()
- elif x == '6':
- bao()
- elif x == '7':
- print('7.退出系统')
- print('欢迎下次使用该系统')
- sys.exit(0)
- else:
- print('输入序号有误,请重新输入')
- continue
- input('....回车键继续操作....')
结合了json文件保存学生信息以及教师的账号密码,以及两种端口,教师以及学生两个登入端口,打破了传统的初始化直接进入系统,同时也带注册功能,方便多人的登入以及管理。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。