文章目录
运行展示: 学了20天的萌新做的学生管理系统
Part 1 主界面
1.1 界面UI
===================================================
** 欢迎来到西川中学学生管理系统 **
◕‿- 按1. 登录
◕‿- 按2. 注册
◕‿- 其他键. 退出
❀谨记戴高龄校长教导:不好好学习就切对门子上大学❀
===================================================
1.2 代码
import os
from teacher_main_page import teacher_main_page
from count_failure import count_tempt
@count_tempt
def login():
"""
这个是登录并判断是否登录成功的函数
:return:True或者False
"""
teacher_infos = eval('[' + open('teacher_info.txt', 'r', encoding='utf-8').read() + ']')
t_username = input('请输入用户名(2~6位):')
t_password = input('请输入密码(6~12位):')
for x in teacher_infos:
if t_username == x['t_username']:
if t_password == x['t_password']:
return teacher_main_page(t_username)
else:
print('密码错误!')
return False
else:
print('该用户名未注册!')
return False
@count_tempt
def register():
"""
这个是注册并判断是否注册成功的函数
:return: True或False
"""
teacher_infos = eval('[' + open('teacher_info.txt', 'r', encoding='utf-8').read() + ']')
t_username = input('请输入中文用户名(2~6位):')
t_password = input('请输入密码(6~12位):')
if not 2 <= len(t_username) <= 6:
print('用户名长度不符合规范!')
return False
elif [x for x in t_username if x < '一' or x > '龥']:
print('用户名只支持中文!')
return False
else:
pass
for single_teacher in teacher_infos:
if single_teacher['t_username'] == t_username:
print('已有该账号!')
return False
if not 6 <= len(t_password) <= 12:
print('密码长度不符合规范!')
return False
open('teacher_info.txt', 'a', encoding='utf-8').write(',{' + f"'t_username':'{t_username}', 't_password':'{t_password}'" + '}')
print('注册成功!')
return start_page()
def start_page():
"""
这个是打印开始页面并得到用户登录/注册/退出选择的函数
:return:无
"""
print(open(r'pages\hello_page', mode='r', encoding='utf-8').read())
if not os.path.exists('teacher_info.txt'):
open('teacher_info.txt', 'w', encoding='utf-8').write(
'{"t_username": "default", "t_password":"default"}')
choice = input('请输入选项:')
# 调用登录函数
if choice == '1':
login()
# 调用注册函数
elif choice == '2':
register()
else:
print('退出成功!')
return
start_page()
Part 2 教师主页面
1.1 界面UI
===================================================
** 欢迎您, %s 老师! **
☞ 按1. 添加学生
☞ 按2. 查看学生
☞ 其他键. 返回
❀谨记戴高龄校长教导:不好好学习就切对门子上大学❀
===================================================
1.2 代码
import os
def teacher_main_page(t_username):
"""
这个是打印教师个人主页面并收集教师接下来要进行的操作的函数
:param t_username:教师用户名
:return:下一个执行的函数
"""
t = open(r'pages\teacher_main_page', encoding='utf-8')
print(t.readline(), end='')
print(t.readline() % (t_username), end='')
print(t.read())
t.close()
# 创建学生信息初始文件
if not os.path.exists(f'students_info\\stu_info_{t_username}.txt'):
open(f'students_info\\stu_info_{t_username}.txt', 'w', encoding='utf-8').write(
'{"s_username": "default", "age":"default", "tel":"default", "num":"stu0000"}')
choice = input('请输入选择:')
if choice == '1':
from add_student import add_stu, add_success
return add_stu(t_username)
elif choice == '2':
from check_func import check_main_page
return check_main_page(t_username)
else:
from stu_manage_system import start_page
return start_page()
Part 3 添加学生功能
def add_success(t_username):
"""
这个是添加成功后让教师可以继续添加或返回教师主页面的函数
:param t_username:教师姓名
:return:添加函数或教师主页面
"""
print('添加成功!\n1. 继续\n2. 返回')
while True:
try:
choice = int(input('请输入选项数字(1~2):'))
if 1 <= choice <= 2:
break
else:
print('只能输入1~2的数字!')
except:
print('只能输入1~2的数字!')
# 调用登录函数
if choice == 1:
add_stu(t_username)
# 调用注册函数
elif choice == 2:
from teacher_main_page import teacher_main_page
teacher_main_page(t_username)
def add_stu(t_username):
"""
这个是添加学生信息的函数
:param t_username: 教师姓名
:return: 无
"""
s_username = input('请输入学生姓名:')
while True:
try:
age = int(input('请输入学生年龄:'))
break
except:
print('输入的年龄不是整数!')
tel = input('请输入学生电话:')
temp = int(
eval("[" + open(f'students_info\\stu_info_{t_username}.txt', encoding='utf-8').read() + "]")[-1]["num"][3:]) + 1
num = f'stu{temp:0>4}'
open(f'students_info\\stu_info_{t_username}.txt', 'a', encoding='utf-8').write(
', {' + f'"s_username": "{s_username}", "age": {age}, "tel": "{tel}", "num": "{num}"' + '}')
add_success(t_username)
Part 4 查看学生功能
4.1 查看界面UI
===================================================
请选择查看方式
➥ 按1. 查看所有学生
➥ 按2. 根据姓名查找
➥ 按3. 根据学号查找
➥ 其他键. 返回
❀谨记戴高龄校长教导:不好好学习就切对门子上大学❀
===================================================
4.2 修改或删除界面UI
===================================================
请选择接下来要进行的操作
♨ 按1. 修改学生信息
♨ 按2. 删除学生
♨ 其他键. 返回
❀谨记戴高龄校长教导:不好好学习就切对门子上大学❀
===================================================
4.3 代码
def check_main_page(t_username):
"""
这个是打印查看学生主页面和收集选项的函数
:param t_username: 教师用户名
:return: 下一个执行的函数
"""
print(open(r'pages\check_page', encoding='utf-8').read())
choice = input('请输入选择(1~4):')
if choice == '1':
return check_all(t_username)
elif choice == '2':
return check_specific_name(t_username)
elif choice == '3':
return check_specific_num(t_username)
else:
from teacher_main_page import teacher_main_page
return teacher_main_page(t_username)
def check_all(t_username):
"""
这是查看所有学生的函数
:param t_username: 教师名
:return: 修改或删除主界面
"""
all_stu_info = eval('[' + open(f'students_info\\stu_info_{t_username}.txt', 'r', encoding='utf-8').read() + ']')
count_index = 0
list_target = []
for single_dict in all_stu_info:
print(f'索引号:{count_index}', end=',')
print(','.join(f'{key}:{value}' for key, value in single_dict.items()))
single_dict["索引号"] = count_index
list_target += [single_dict]
count_index += 1
return change_del_main_page(t_username, list_target)
def check_specific_name(t_username):
"""
这是按姓名查找学生的函数
:param t_username: 教师名
:return: 修改或删除主界面
"""
all_stu_info = eval('[' + open(f'students_info\\stu_info_{t_username}.txt', 'r', encoding='utf-8').read() + ']')
check_name = input('请输入要查询学生的姓名:')
count_index = 0
list_target = []
for single_dict in all_stu_info:
if single_dict['s_username'] == check_name:
print(f'索引号:{count_index}', end=',')
print(','.join(f'{key}:{value}' for key, value in single_dict.items()))
single_dict["索引号"] = count_index
list_target += [single_dict]
count_index += 1
return change_del_main_page(t_username, list_target)
def check_specific_num(t_username):
"""
这是按学号查找学生的函数
:param t_username: 教师名
:return: 修改或删除主界面
"""
all_stu_info = eval('[' + open(f'students_info\\stu_info_{t_username}.txt', 'r', encoding='utf-8').read() + ']')
check_num = input('请输入要查询学生的学号:')
count_index = 0
list_target = []
for single_dict in all_stu_info:
if single_dict['num'] == check_num:
print(f'索引号:{count_index}', end=',')
print(','.join(f'{key}:{value}' for key, value in single_dict.items()))
single_dict["索引号"] = count_index
list_target += [single_dict]
count_index += 1
return change_del_main_page(t_username, list_target)
def change_del_main_page(t_username, list_target):
"""
这是打印修改或删除主界面并收集选项的函数
:param t_username: 教师名
:param list_target: 上一步查看得到的学生信息列表
:return: 修改或删除函数
"""
print(open(r'pages\change_or_del', mode='r', encoding='utf-8').read())
choice = input('请输入选项(1、2或其他):')
if choice == '1':
from change_stu_info import change_info
return change_info(t_username, list_target)
elif choice == '2':
from del_stu_info import del_info
return del_info(t_username, list_target)
else:
return check_main_page(t_username)
Part 5 修改学生信息功能
5.1 界面UI
===================================================
请选择接下来要修改的信息
✧ 按1. 修改学生姓名
✧ 按2. 修改学生年龄
✧ 按3. 修改学生电话
✧ 其他键. 返回
❀谨记戴高龄校长教导:不好好学习就切对门子上大学❀
===================================================
5.2 代码
def change_info(t_username, list_target):
while True:
try:
search_code = int(input('请输入要修改学生的索引号:'))
if search_code <= list_target[-1]["索引号"]:
break
else:
print('输入的索引号必须在上面范围里!')
except:
print('索引号必须是数字!')
for x in list_target:
if search_code == x["索引号"]:
num_target = x["num"]
old_list = eval('[' + open(f'students_info\\stu_info_{t_username}.txt', encoding='utf-8').read() + ']')
print(open('pages\\change_choice_page', encoding='utf-8').read())
choice = input('请选择要修改的信息:')
if choice == '1':
new_name = input('请输入新的姓名:')
for x in old_list:
if x["num"] == num_target:
x["s_username"] = new_name
elif choice == '2':
while True:
try:
new_age = int(input('请输入新的年龄:'))
break
except:
print('年龄只能是整数!')
for x in old_list:
if x["num"] == num_target:
x["age"] = new_age
elif choice == '3':
new_tel = input('请输入新的电话:')
for x in old_list:
if x["num"] == num_target:
x["tel"] = new_tel
else:
from teacher_main_page import teacher_main_page
return teacher_main_page(t_username)
open(f'students_info\\stu_info_{t_username}.txt', mode='w', encoding='utf-8').write(f'{old_list}'[1:-1])
print('修改成功!')
input('输入任意键返回查看界面')
from check_func import check_main_page
return check_main_page(t_username)
Part 6 删除学生功能
def del_info(t_username, list_target):
while True:
try:
search_code = int(input('请输入要删除学生的索引号:'))
if search_code <= list_target[-1]["索引号"]:
break
else:
print('输入的索引号必须在上面范围里!')
except:
print('索引号必须是数字!')
for x in list_target:
if search_code == x["索引号"]:
num_target = x["num"]
old_list = eval('[' + open(f'students_info\\stu_info_{t_username}.txt', encoding='utf-8').read() + ']')
new_list = [x for x in old_list if not x["num"] == num_target]
open(f'students_info\\stu_info_{t_username}.txt', mode='w', encoding='utf-8').write(f'{new_list}'[1:-1])
print('删除成功!')
input('输入任意键返回查看界面')
from check_func import check_main_page
return check_main_page(t_username)
Part 附录 统计失败次数装饰器
def count_tempt(f):
"""
这个是统计错误次数的装饰器
:param f:原函数
:return:新函数
"""
def new_f(*args, **kwargs):
tempt = 1
while tempt <= 5:
result = f(*args, **kwargs)
if result == False:
print(f'失败次数:{tempt}')
tempt += 1
else:
return result
print('失败次数过多,请稍后尝试!')
return result
return new_f