制作一个学生信息管理系统
具有以下几种功能
# stu.define.py
class Student:
def __init__(self, stu_id, name, eng_score, python_score, java_score, total_score):
self.stu_id = stu_id
self.name = name
self.eng_score = eng_score
self.python_score = python_score
self.java_score = java_score
self.total_score = total_score
def __str__(self):
return f'{self.stu_id}\t{self.name}\t{self.eng_score}\t{self.python_score}\t{self.java_score}\t{self.total_score}'
# func_define.py
from stu_define import *
import re
heading = 'ID\t姓名\t英语\tPy\tJava\t总成绩\n'
path = 'D:\\dev\\python-learn\\学生成绩汇总.txt'
def type_in_stu_info():
stu_id = input('请输入ID(如1001):')
name = input('请输入姓名:')
eng_score = input('请输入英语成绩:')
python_score = input('请输入Python成绩:')
java_score = input('请输入Java成绩:')
try:
total_score = int(eng_score) + int(python_score) + int(java_score)
except Exception:
err()
continue_type_in()
student = Student(stu_id, name, eng_score, python_score, java_score, str(total_score))
try:
with open(path, 'r', encoding='UTF-8') as f:
pass
except Exception:
with open(path, 'w', encoding='UTF-8') as f:
f.write(heading)
finally:
with open(path, 'a', encoding='UTF-8') as f:
f.write(f'{str(student)}\n')
continue_type_in()
def continue_type_in():
order = input('是否继续添加?y/n\n')
if order in ['y', 'yes']:
type_in_stu_info()
elif order in ['n', 'no']:
print('学生信息录入完毕!!!')
menu()
else:
err()
continue_type_in()
def find_stu():
try:
f = open(path, 'r', encoding='UTF-8')
f.close()
except Exception:
print('学生信息未录入,请先录入学生信息!')
menu()
with open(path, 'r', encoding='UTF-8') as f:
order = input('按ID查找请输入1,按姓名查找请输入2:')
if order == '1':
find_id = input('请输入学生ID:')
flag = 0
for line in f.readlines():
get_id = line.strip().replace('\t', ',').split(',')
if get_id[0] == find_id:
print(f'{heading}{line}')
flag = 1
if not flag:
print('没有查找到该学生')
continue_find_stu()
elif order == '2':
find_name = input('请输入学生姓名:')
flag = 0
for line in f.readlines():
get_name = line.strip().replace('\t', ',').split(',')
if get_name[1] == find_name:
print(f'{heading}{line}')
flag = 1
if not flag:
print('没有查找到该学生')
continue_find_stu()
else:
err()
find_stu()
def continue_find_stu():
order = input('是否继续查找?y/n\n')
if order in ['y', 'yes']:
find_stu()
elif order in ['n', 'no']:
print('查找完毕!!!')
else:
err()
continue_find_stu()
def del_stu():
try:
f = open(path, 'r', encoding='UTF-8')
f.close()
except Exception:
print('学生信息未录入,请先录入学生信息!')
menu()
with open(path, 'r', encoding='UTF-8') as f:
lines = f.readlines()
find_id = input('请输入删除学生ID:')
flag = 0
with open(path, 'w', encoding='UTF-8') as f:
for line in lines:
if re.search(find_id, line):
flag = 1
print(f'ID为{find_id}的学生信息已删除!')
else:
f.write(line)
if flag:
show_all()
else:
print('没有查找到该学生')
continue_del_stu()
def continue_del_stu():
cmd = input('是否继续删除学生?y/n\n')
if cmd in ['y', 'yes']:
del_stu()
elif cmd in ['n', 'no']:
print('删除完毕!')
else:
err()
continue_find_stu()
def change_stu():
try:
f = open(path, 'r', encoding='UTF-8')
f.close()
except Exception:
print('学生信息未录入,请先录入学生信息!')
menu()
show_all()
find_id = input('请输入要修改的学生ID:')
flag = 0
with open(path, 'r', encoding='UTF-8') as f:
lines = f.readlines()
with open(path, 'w', encoding='UTF-8') as f:
for line in lines:
if re.search(find_id, line):
print('找到这名学生,可以修改他的相关信息了')
stu_id = input('请输入ID(如1001):')
name = input('请输入姓名:')
eng_score = input('请输入英语成绩:')
python_score = input('请输入Python成绩:')
java_score = input('请输入Java成绩:')
total_score = int(eng_score) + int(python_score) + int(java_score)
student = Student(stu_id, name, eng_score, python_score, java_score, str(total_score))
f.write(f'{str(student)}\n')
flag = 1
print(f'学生信息已修改!')
else:
f.write(line)
if flag:
show_all()
else:
print('没有查找到该学生')
continue_change_stu()
def continue_change_stu():
cmd = input('是否继续修改学生信息?y/n\n')
if cmd in ['y', 'yes']:
change_stu()
elif cmd in ['n', 'no']:
print('修改完毕!')
else:
err()
continue_change_stu()
def sort_stu():
try:
f = open(path, 'r', encoding='UTF-8')
f.close()
except Exception:
print('学生信息未录入,请先录入学生信息!')
menu()
show_all()
cmd = input('请选择(0升序,1降序):')
if cmd not in ['0', '1']:
err()
continue_sort_stu()
sort_subject = input('请选择排序方式(1.英语,2.Python,3.Java,4.总成绩):')
if sort_subject not in ['1', '2', '3', '4']:
err()
continue_sort_stu()
score = []
with open(path, 'r', encoding='UTF-8') as f:
for line in f.readlines():
get_score = line.strip().replace('\t', ',').split(',')
score.append(get_score)
del score[0] # 删掉第一行抬头
def sort_func(command, sub):
sub_dict = {'1': 2, '2': 3, '3': 4, '4': 5}
command_dict = {'0': False, '1': True}
score.sort(key=lambda subject: subject[sub_dict[sub]], reverse=command_dict[command])
with open(path, 'w', encoding='UTF-8') as f:
f.write(heading)
index = 0
while index < len(score):
student = Student(score[index][0], score[index][1], score[index][2], score[index][3], score[index][4],
score[index][5])
f.write(f'{str(student)}\n')
index += 1
sort_func(cmd, sort_subject)
show_all()
def continue_sort_stu():
cmd = input('是否继续排序?y/n\n')
if cmd in ['y', 'yes']:
sort_stu()
elif cmd in ['n', 'no']:
print('排序完毕!')
menu()
else:
err()
continue_sort_stu()
def stu_num():
with open(path, 'r', encoding='UTF-8') as f:
print(f'学生总人数为{len(f.readlines()) - 1}人')
def show_all():
try:
with open(path, 'r', encoding='UTF-8') as f:
for line in f.readlines():
print(line.strip())
except Exception:
print('学生信息未录入,请先录入学生信息!')
def err():
print('输入错误,请重新输入!')
def exit_system():
print('退出系统!')
exit()
def menu():
while True:
print("""
===============学生信息管理系统===============
------------------功能菜单-------------------
1.录入学生信息
2.查找学生信息
3.删除学生信息
4.修改学生信息
5.排序
6.统计学生总人数
7.显示所有学生信息
0.退出系统
--------------------------------------------
""")
command = input('请选择:')
if command == '1':
type_in_stu_info()
elif command == '2':
find_stu()
elif command == '3':
del_stu()
elif command == '4':
change_stu()
elif command == '5':
sort_stu()
elif command == '6':
stu_num()
elif command == '7':
show_all()
elif command == '0':
exit_system()
else:
err()
if __name__ == '__main__':
menu()
# stu_system_mian.py
from stu_define import *
from func_define import *
menu()