学生管理系统
- 1.首先新建一个index.py和student_manager.py文件
- 2.新建一个file_manager.py 文件
- 3.新建一个model.py
- 4.新建一个tools类
- 5.在当前目录下面新建一个files文件夹存放文件
- 6.开始刺激的填充代码
- 先看看效果
- 结果图
- 文件路径
1.首先新建一个index.py和student_manager.py文件
用来写本系统的基本思路,然后慢慢的填充代码
def register():
pass
def login():
pass
def start():
content = file_manager.read_file('welcome.txt')
while True:
operator = input(content + '\n请选择(1~3):')
if operator =='1':
login()
print('登录')
elif operator =='2':
register()
print('注册')
elif operator =='3':
# break #把死循环停止
exit(0) #结束程序
else:
print('输入有误')
# 读文件
# try:
# with open('files/welcome.txt', 'r', encoding='utf-8') as file:
# content = file.read()
# while True:
# input(content)
# except FileNotFoundError:
# print('文件未找到')
#
if __name__ == '__main__':
start()
'''
学生管理类
show_manager 显示学生界面
对学生信息的增删改查
'''
def add_student():
pass
def show_student( ):
pass
def modify_student():
pass
def delte_student():
pass
def show_manager():
content = file_manager.read_file('student.txt')%name
while True:
print(content)
operator = input('请选择功能(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delte_student()
elif operator == '5':
break
else:
print('输入有误')
由于读写文件经常使用,将文件读写放在一个类中方便调用,需要的有如下三个
'''
文件管理类
读文件
read_file 读TXT文件
write_json写json文件
read_json 读json文件
'''
base_dir = './files/'
def read_file(file_name):
try:
with open(base_dir+file_name,'r',encoding='utf-8') as file:
content = file.read()
return content
except FileNotFoundError:
print('文件未找到')
def write_json(file_name,data):
with open(base_dir+file_name,'w',encoding='utf8') as file:
import json
json.dump(data,file)
def read_json(file_name,default_data):
try:
with open(base_dir+file_name,'r',encoding='utf-8') as file:
import json
return json.load(file)
except FileNotFoundError:
return default_data
用于创建对象,需要的如下
'''
model 类
用于创建对象
'''
class Teacher(object):
def __init__(self,name,password):
import tools
self.name = name
self.password =tools.encrypt_password(password)
class Student(object):
def __init__(self,s_id,name,age,gender,tel):
self.s_id = s_id
self.name = name
self.age = age
self.gender = gender
self.tel = tel
用来加密密码
'''
工具类
encrypt_password 加密
'''
import hashlib
def encrypt_password(passwd,x='ztwztw'):
h = hashlib.sha256()
h.update(passwd.encode('utf8'))
h.update(x.encode('utf8'))
return h.hexdigest()
㊥㊦㊧㊨㊒㊞㊑㊒㊓㊔㊕㊖㊗㊘㊜㊝㊟㊠㊡㊢㊩㊪㊫㊬
✪✪✪✪✪欢迎来到学生管理系统✪✪✪✪✪
◆1.登录
◆2.注册
◆3.退出
ℬℰℯℱℊℋℎℐℒℓℳℴ℘ℛℭ℮ℌℑℜℨ♪♫♩♬♭♮♯°øⒶ✪✪✪✪✪✪
ℬℰℯℱℊℋℎℐℒℓℳℴ℘ℛℭ℮ℌℑℜℨ♪♫♩♬♭♮♯°øⒶ✪✪✪✪✪✪
❤❤❤❤欢迎%s老师进入到学生管理系统:
①.添加学生❤
②.查看学生❤
③.修改学生信息❤
④.删除学生❤
⑤.返回❤
㊥㊦㊧㊨㊒㊞㊑㊒㊓㊔㊕㊖㊗㊘㊜㊝㊟㊠㊡㊢㊩㊪㊫㊬
6.开始刺激的填充代码由于比较多,就不一一展示填充了
- inedex
import file_manager
import model
import tools
import student_manager
def register():
# 读取文件,查看文件里是否有数据。塑果文件不存在,默认是一个字典
data = file_manager.read_json('data.json',{})
while True:
teacher_name = input('请输入账号(3~6位):')
if not 3 <= len(teacher_name) <= 6:
print('输入账号不符合要求 请重新输入')
else:
break
if teacher_name in data:
print('注册失败 该账号已注册')
while True:
password = input('请输入密码(6~12位):')
if not 6 <= len(password) <= 12:
print('输入账号不符合要求 请重新输入')
else:
break
t = model.Teacher(teacher_name,password)
data[t.name] = t.password
file_manager.write_json('data.json',data)
print('注册成功卐 账号:',data)
def login():
data = file_manager.read_json('data.json',{})
teacher_name = input('请输入你的用户名:')
password = input('请输入你的密码:')
if teacher_name not in data:
print('登录失败 账号未注册')
return
if data[teacher_name] ==tools.encrypt_password(password):
student_manager.name = teacher_name
student_manager.show_manager()
print('登录成功')
else:
print('登录失败')
def start():
content = file_manager.read_file('welcome.txt')
while True:
operator = input(content + '\n请选择(1~3):')
if operator =='1':
login()
print('登录')
elif operator =='2':
register()
print('注册')
elif operator =='3':
exit(0)
else:
print('输入有误')
if __name__ == '__main__':
start()
'''
学生管理类
show_manager 显示学生界面
对学生信息的增删改查
'''
import file_manager
import model
name = ''
def add_student():
x= file_manager.read_json(name+'.json',{})
if not x:
students = []
num = 0
else:
students = x['all_student']
num = int(x['num'])
while True:
s_name = input('请输入学生姓名:')
s_age = input('请输入年龄:')
s_gender = input('请输入性别:')
s_tel = input('请输入电话号码:')
num +=1
s_id = 'stu_'+str(num).zfill(4)
s = model.Student(s_id,s_name,s_age,s_gender,s_tel)
students.append(s.__dict__)
data = {'all_student':students,'num':len(students)}
file_manager.write_json(name+'.json',data)
choice = input('添加成功!\n1.继续\n2.返回\n请选择(1~2):')
if choice =='2':
return
else:
print('输入有误')
def show_student( ):
x = input('1.查看所有学生\n2.根据姓名查找\n3.根据学号查找\n其他:返回\n请选择:')
y = file_manager.read_json(name+'.json',{})
students = y.get('all_student',[])
if not students:
print('该老师还没有学生')
return
if x == '1':
pass
elif x=='2':
s_name = input('请输入名字:')
students = filter(lambda s: s['name'] == s_name,students)
elif x =='3':
s_id = input('请输入学号:')
students = filter(lambda s: s['s_id'] == s_id, students)
else:
return
if not students:
print('未找到')
for student in students:
print('学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, s:{tel}'.format(**student))
def modify_student():
global student
y = file_manager.read_json(name + '.json', {})
all_students = y.get('all_student', [])
key = value = ''
if not all_students:
print('该老师还没有学生')
return
num = input('按姓名修改\n yes or no :')
if num == 'yes':
key = 'name'
value = input('请输 入要修改的学生名字:')
else:
return
students = list(filter(lambda s: s.get(key, '') == value, all_students))
if not students:
print('没有找到对应的学生')
return
for i,student in enumerate(students):
print('{x} 学号:{s_id},姓名:{name},性别:{gender},年龄:{age}, s:{tel}'.format(x=i,**student))
n = input('请输入需要修改的学生的标号(0~{}),q-返回:'.format(i))# 使用变量i有潜在风险
if not n.isdigit() or not 0 <= int(n) <=i:
print('输入类容不合法')
return
# 将学生从all_student列表修改
new_name = input('请重新输入学生姓名:')
for u in students:
if u['name'] == new_name:
print('修改失败,新的用户名已经存在')
return
new_age = input('请重新输入年龄:')
new_gender = input('请重新输入性别:')
new_ter = input('请重新输入电话号码:')
n = int(n)
user = students[n]
user['name'] = new_name
user['age'] = new_age
user['genter'] = new_gender
user['tel'] = new_ter
print('修改成功')
print('{x} 学号:{s_id},姓名:{name},性别:{gender},年龄:{age}, s:{tel}'.format(x=i,**user))
y['all_student'] =all_students
file_manager.write_json(name+'.json',y)
def delte_student():
y = file_manager.read_json(name + '.json', {})
all_students = y.get('all_student', [])
key = value = ''
if not all_students:
print('该老师还没有学生')
return
num = input( '1.按姓名删\n2.按学号删\n其他:返回\n请选择:')
if num =='1':
key = 'name'
value = input('请输 入要删除的学生名字:')
elif num=='2':
key = 's_id'
value = input('请输入要删除的学生id:')
else:
return
students = list(filter(lambda s:s.get(key,'') == value,all_students))
if not students:
print('没有找到对应的学生')
return
for i,student in enumerate(students):
print('{x} 学号:{s_id},姓名:{name},性别:{gender},年龄:{age}, s:{tel}'.format(x=i,**student))
n = input('请输入需要删除的学生的标号(0~{}),q-返回:'.format(i))# 使用变量i有潜在风险
if not n.isdigit() or not 0 <= int(n) <=i:
print('输入类容不合法')
return
# 将学生从all_student列表删除
all_students.remove(students[int(n)])
print('删除成功')
for i, student in enumerate(students):
print('{x} 学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, s:{tel}'.format(x=i, **student))
y['all_student'] = all_students
file_manager.write_json(name+'.json',y)
def show_manager():
content = file_manager.read_file('student.txt')%name
while True:
print(content)
operator = input('请选择功能(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delte_student()
elif operator == '5':
break
else:
print('输入有误')