准备文件students_page.txt
和welcome.txt
用来显示系统界面。
思路:
创建index.py
主程序。
创建file_manager.py
用来存放处理文件读写相关的方法,使整个结构清晰。
index.py
import file_manager
def start():
content = file_manager.read_file('welcome.txt')
while 1:
operator = input(content + '\n请选择(1-3):')
if operator == '1':
print('登录')
elif operator == '2':
print('注册')
elif operator == '3':
print('退出')
else:
print('输入有误')
if __name__ == '__main__':
start()
file_manager.py
base_dir = './files/'
def read_file(file_name):
try:
with open(base_dir + file_name, 'r', encoding='utf8') as file:
content = file.read()
return content
except FileNotFoundError:
print('文件未找到')
2. 退出功能实现
可以用3种方法实现,index.py
代码如下:
import sys
import file_manager
def start():
content = file_manager.read_file('welcome.txt')
while 1:
operator = input(content + '\n请选择(1-3):')
if operator == '1':
print('登录')
elif operator == '2':
print('注册')
elif operator == '3':
# exit(0)
# break
sys.exit(0)
else:
print('输入有误')
if __name__ == '__main__':
start()
3. 注册功能实现
思路:
输入账号、密码并判断合法性。 读取data.json
文件,如果文件不存在,默认是一个空字典。
创建一个Teacher类的对象,通过对象将账号、密码保存到字典。这里不创建对象,直接使用data[teacher_name] = password
也可以。
将密码加密(创建一个新的模块tools.py
)。
index.py
import sys
import file_manager
import model
def register():
# 读取文件,查看文件里面是否有数据,如果文件不存在,默认是一个空字典
data = file_manager.read_json('data.json')
while 1:
teacher_name = input('请输入账号(3~6位):')
if not 3 <= len(teacher_name) <= 6:
print('账号不符合要求,请重新输入!')
else:
break
if teacher_name in data:
print('注册失败,该账户已经注册!')
return
while 1:
password = input('请输入密码(6~12位):')
if not 6 <= len(password) <= 12:
print('密码不符合要求,请重新输入!')
else:
break
# 账号和密码都已经输入正确后,将数据进行保存
t = model.Teacher(teacher_name, password)
data[t.name] = t.password
# data[teacher_name] = password
file_manager.write_json('data.json', data)
def login():
pass
def start():
content = file_manager.read_file('welcome.txt')
while 1:
operator = input(content + '\n请选择(1-3):')
if operator == '1':
login()
elif operator == '2':
register()
elif operator == '3':
# exit(0)
# break
sys.exit(0)
else:
print('输入有误')
if __name__ == '__main__':
start()
file_manager.py
base_dir = './files/'
def read_file(file_name):
try:
with open(base_dir + file_name, 'r', encoding='utf8') 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='utf8') as file:
import json
return json.load(file)
except FileNotFoundError:
return default_data
model.py
import tools
class Teacher(object):
def __init__(self, name, password):
self.name = name
self.password = tools.encrypt_password(password)
tools.py
import hashlib
def encrypt_password(passwd, x='asdfgh'):
h = hashlib.sha256()
h.update(passwd.encode('utf8'))
h.update(x.encode('utf8'))
return h.hexdigest()
4. 登录功能实现
思路:
读取文件,判断输入的老师账号是否存在。 输入密码,并将输入的密码加密后进行比对,判断密码是否正确。 登录成功后,显示学生管理页面。 index.pyimport sys
import file_manager
import model
import student_manager
def register():
# 读取文件,查看文件里面是否有数据,如果文件不存在,默认是一个空字典
data = file_manager.read_json('data.json')
while 1:
teacher_name = input('请输入账号(3~6位):')
if not 3 <= len(teacher_name) <= 6:
print('账号不符合要求,请重新输入!')
else:
break
if teacher_name in data:
print('注册失败,该账户已经注册!')
return
while 1:
password = input('请输入密码(6~12位):')
if not 6 <= len(password) <= 12:
print('密码不符合要求,请重新输入!')
else:
break
# 账号和密码都已经输入正确后,将数据进行保存
t = model.Teacher(teacher_name, password)
data[t.name] = t.password
# data[teacher_name] = password
file_manager.write_json('data.json', data)
def login():
data = file_manager.read_json('data.json')
teacher_name = input('请输入老师账号:')
if teacher_name not in data:
print('登录失败,该账号没有注册!')
return
password = input('请输入密码:')
import tools
if data[teacher_name] == tools.encrypt_password(password):
student_manager.teacher_name = teacher_name
student_manager.show_manager()
else:
print('密码错误,登录失败!')
def start():
content = file_manager.read_file('welcome.txt')
while 1:
operator = input(content + '\n请选择(1-3):')
if operator == '1':
login()
elif operator == '2':
register()
elif operator == '3':
# exit(0)
# break
sys.exit(0)
else:
print('输入有误')
if __name__ == '__main__':
start()
student_manager.py
import file_manager
teacher_name = ''
def show_manager():
# 显示学生管理页面
content = file_manager.read_file('students_page.txt').format(teacher_name)
while 1:
print(content)
input('请选择(1~5):')
5. 添加学生功能实现
学生管理系统的数据结构设置:
一个学生信息对应一个字典 整个系统的所有学生:所有学生对应一个列表,列表中的元素全是字典 整个系统:一个字典, 字典中有一个key(all_student),对应的值是所有学生 这个字典需要做数据持久化,将数据存储到json
文件中, 文件名就是当前登录的账号名
比如jack.json
表示jack老师的所有学生:
{
'all_student': [
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'},
{'name': '张三', 'age': 89, 'tel': '237837293'}
],
'num': 4
}
思路:
输入学生信息 创建一个Student类 将学生信息保存到字典中 把字典写入文件里 student_manager.pyimport file_manager
import model
teacher_name = ''
def add_student():
"""添加学生"""
x = file_manager.read_json(teacher_name + '.json')
if not x:
students = []
num = 0
else:
students = x['all_student']
num = int(x['num'])
while 1:
s_name = input('请输入学生姓名:')
s_age = input('请输入学生年龄:')
s_gender = input('请输入学生性别:')
s_tel = input('请输入学生电话:')
num += 1
# zifll()方法,在字符串的前面补0
s_id = 'stu' + str(num).zfill(4)
# 创建一个Student对象
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(teacher_name + '.json', data)
choice = input('添加成功!\n1.继续\n2.返回\n请选择(1~2):')
if choice == '2':
break
def show_student():
"""查看学生"""
pass
def modify_student():
"""修改学生信息"""
pass
def delete_student():
"""删除学生"""
pass
def show_manager():
# 显示学生管理页面
content = file_manager.read_file('students_page.txt').format(teacher_name)
while 1:
print(content)
operator = input('请选择(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delete_student()
elif operator == '5':
break
else:
print('输入有误!')
model.py
import tools
class Teacher(object):
def __init__(self, name, password):
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
6. 查找学生功能实现
思路:
读取文件,查找所有学生,并打印。 按姓名查找。 按学号查找。 student_manager.pyimport file_manager
import model
teacher_name = ''
def add_student():
"""添加学生"""
x = file_manager.read_json(teacher_name + '.json')
if not x:
students = []
num = 0
else:
students = x['all_student']
num = int(x['num'])
while 1:
s_name = input('请输入学生姓名:')
s_age = input('请输入学生年龄:')
s_gender = input('请输入学生性别:')
s_tel = input('请输入学生电话:')
num += 1
# zifll()方法,在字符串的前面补0
s_id = 'stu' + str(num).zfill(4)
# 创建一个Student对象
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(teacher_name + '.json', data)
choice = input('添加成功!\n1.继续\n2.返回\n请选择(1~2):')
if choice == '2':
break
def show_student():
"""查找学生"""
key = value = ''
x = input('1.查找所有学生\n2.根据姓名查找\n3.根据学号查找\n其他:返回\n请选择:')
y = file_manager.read_json(teacher_name + '.json')
students = y.get('all_student', [])
if not students:
print('该老师还没有学员,请添加学员')
return
if x == '1':
pass
elif x == '2':
value = input('请输入学员姓名:')
key = 'name'
elif x == '3':
value = input('请输入学员的id:')
key = 's_id'
else:
return
students = filter(lambda s: s.get(key, '') == value, students)
if not students:
print('未找到学员')
return
for student in students:
print('学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, ☎:{tel}'.format(**student))
def modify_student():
"""修改学生信息"""
pass
def delete_student():
"""删除学生"""
pass
def show_manager():
# 显示学生管理页面
content = file_manager.read_file('students_page.txt').format(teacher_name)
while 1:
print(content)
operator = input('请选择(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delete_student()
elif operator == '5':
break
else:
print('输入有误!')
7. 删除学生功能实现
思路:
读取文件。 按姓名删除。 按学号删除。注意:每个学生信息前面有标号,按照标号修改,就可以不用担心重名了删除哪个学生的问题。
student_manager.pyimport file_manager
import model
teacher_name = ''
def add_student():
"""添加学生"""
x = file_manager.read_json(teacher_name + '.json')
if not x:
students = []
num = 0
else:
students = x['all_student']
num = int(x['num'])
while 1:
s_name = input('请输入学生姓名:')
s_age = input('请输入学生年龄:')
s_gender = input('请输入学生性别:')
s_tel = input('请输入学生电话:')
num += 1
# zifll()方法,在字符串的前面补0
s_id = 'stu' + str(num).zfill(4)
# 创建一个Student对象
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(teacher_name + '.json', data)
choice = input('添加成功!\n1.继续\n2.返回\n请选择(1~2):')
if choice == '2':
break
def show_student():
"""查找学生"""
key = value = ''
x = input('1.查找所有学生\n2.根据姓名查找\n3.根据学号查找\n其他:返回\n请选择:')
y = file_manager.read_json(teacher_name + '.json')
students = y.get('all_student', [])
if not students:
print('该老师还没有学员,请添加学员')
return
if x == '1':
pass
elif x == '2':
value = input('请输入学员姓名:')
key = 'name'
elif x == '3':
value = input('请输入学员的id:')
key = 's_id'
else:
return
students = filter(lambda s: s.get(key, '') == value, students)
if not students:
print('未找到学员')
return
for student in students:
print('学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, ☎:{tel}'.format(**student))
def modify_student():
"""修改学生信息"""
pass
def delete_student():
"""删除学生"""
y = file_manager.read_json(teacher_name + '.json')
all_students = y.get('all_student', [])
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}, ☎:{tel}'.format(x=i, **student))
n = input('请输入要删除的学生的标号(0~{}),q-返回:'.format(len(students) - 1))
if not n.isdigit() or not 0 <= int(n) <= len(students) - 1:
print('输入的内容不合法')
return
# 将学生从all_students里删除
all_students.remove(students[int(n)])
y['all_student'] = all_students
file_manager.write_json(teacher_name + '.json', y)
def show_manager():
# 显示学生管理页面
content = file_manager.read_file('students_page.txt').format(teacher_name)
while 1:
print(content)
operator = input('请选择(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delete_student()
elif operator == '5':
break
else:
print('输入有误!')
8. 修改学生功能实现
思路:
读取文件。 按姓名修改。 按学号修改。注意:每个学生信息前面有标号,按照标号修改,就可以不用担心重名了修改哪个学生的问题。
student_manager.pyimport file_manager
import model
teacher_name = ''
def add_student():
"""添加学生"""
x = file_manager.read_json(teacher_name + '.json')
if not x:
students = []
num = 0
else:
students = x['all_student']
num = int(x['num'])
while 1:
s_name = input('请输入学生姓名:')
s_age = input('请输入学生年龄:')
s_gender = input('请输入学生性别:')
s_tel = input('请输入学生电话:')
num += 1
# zifll()方法,在字符串的前面补0
s_id = 'stu' + str(num).zfill(4)
# 创建一个Student对象
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(teacher_name + '.json', data)
choice = input('添加成功!\n1.继续\n2.返回\n请选择(1~2):')
if choice == '2':
break
def show_student():
"""查找学生"""
key = value = ''
x = input('1.查找所有学生\n2.根据姓名查找\n3.根据学号查找\n其他:返回\n请选择:')
y = file_manager.read_json(teacher_name + '.json')
students = y.get('all_student', [])
if not students:
print('该老师还没有学员,请添加学员')
return
if x == '1':
pass
elif x == '2':
value = input('请输入学员姓名:')
key = 'name'
elif x == '3':
value = input('请输入学员的id:')
key = 's_id'
else:
return
students = filter(lambda s: s.get(key, '') == value, students)
if not students:
print('未找到学员')
return
for student in students:
print('学号:{s_id}, 姓名:{name},性别:{gender},年龄:{age}, ☎:{tel}'.format(**student))
def modify_student():
"""修改学生信息"""
y = file_manager.read_json(teacher_name + '.json')
all_students = y.get('all_student', [])
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}, ☎:{tel}'.format(x=i, **student))
n = input('请输入要修改的学生的标号(0~{}),q-返回:'.format(len(students) - 1))
if not n.isdigit() or not 0 <= int(n) <= len(students) - 1:
print('输入的内容不合法')
return
index = all_students.index(students[int(n)])
students[int(n)]['name'] = input('请输入新的姓名:')
students[int(n)]['gender'] = input('请输入新的性别:')
students[int(n)]['age'] = input('请输入新的年龄:')
students[int(n)]['tel'] = input('请输入新的电话:')
all_students[index] = students[int(n)]
y['all_student'] = all_students
file_manager.write_json(teacher_name + '.json', y)
def delete_student():
"""删除学生"""
y = file_manager.read_json(teacher_name + '.json')
all_students = y.get('all_student', [])
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}, ☎:{tel}'.format(x=i, **student))
n = input('请输入要删除的学生的标号(0~{}),q-返回:'.format(len(students) - 1))
if not n.isdigit() or not 0 <= int(n) <= len(students) - 1:
print('输入的内容不合法')
return
# 将学生从all_students里删除
all_students.remove(students[int(n)])
y['all_student'] = all_students
file_manager.write_json(teacher_name + '.json', y)
def show_manager():
# 显示学生管理页面
content = file_manager.read_file('students_page.txt').format(teacher_name)
while 1:
print(content)
operator = input('请选择(1~5):')
if operator == '1':
add_student()
elif operator == '2':
show_student()
elif operator == '3':
modify_student()
elif operator == '4':
delete_student()
elif operator == '5':
break
else:
print('输入有误!')