学生信息管理系统
- 需求分析
- 系统设计
- 系统开发必备
- 主函数设计
- 学生学习维护模块设计
- 查询/统计模块设计
- 排序模块设计
- 项目打包
完整代码
import os.path
filename = 'student.txt'
def main():
while True:
menm()
choice = int(input('请选择:'))
if choice in [0, 1, 2, 3, 4, 5, 6, 7]:
if choice == 0:
answer = input('您确定要退出系统吗?(y/n)')
if answer == 'y' or answer == 'Y':
print('谢谢你的使用!!!')
break
if answer == 'n' or answer == 'N':
continue
else:
print('你的输入有误,返回菜单')
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
sort()
elif choice == 6:
total()
elif choice == 7:
show()
def menm():
print("==============学生信息管理系统==============")
print("..................功能菜单..................")
print('\t\t\t\t1.录入学生信息')
print('\t\t\t\t2.查找学生信息')
print('\t\t\t\t3.删除学生信息')
print('\t\t\t\t4.修改学生信息')
print('\t\t\t\t5.\t排序')
print('\t\t\t\t6.统计学生总人数')
print('\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t0.\t退出程序')
def insert():
student_list = []
while True:
id = input('请输入学生ID:')
if not id:
break
name = input('请输入学生姓名:')
if not name:
break
try:
english = int(input('请输入英语成绩:'))
math = int(input('请输入数学成绩:'))
chinese = int(input('请输入语文成绩:'))
scores = english + math + chinese
except:
print('输入无效请重新输入')
continue
student = {'id': id, 'name': name, 'english': english, 'math': math, 'chinese': chinese, 'scores': scores}
student_list.append(student)
answer = input('是否继续添加学生?(y/n)')
if answer == 'y' or answer == 'Y':
continue
else:
break
save(student_list)
print('学生信息录入完毕.....')
def save(list):
try:
stu_txt = open(filename, 'a', encoding='utf-8')
except:
stu_txt = open(filename, 'w', encoding='utf-8')
for item in list:
stu_txt.write(str(item) + '\n')
stu_txt.close()
def search():
student_id = input('请输入你要查看的学生id:')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
i = 0
for item in student_md:
isExist = True
d = dict(eval(item))
if d['id'] == student_id:
isExist = False
print('\tid:\t\t' + d['id'])
print('\t姓名:\t' + d['name'])
print('\t英语成绩:' + str(d['english']))
print('\t数学成绩:' + str(d['math']))
print('\t语文成绩:' + str(d['chinese']))
print('\t总成绩:' + str(d['scores']))
break
if isExist:
print(f'id为{student_id}的同学不存在')
else:
return
def delete():
while True:
student_id = input('请输入删除学生的id:')
if student_id != "":
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as file:
student_md = file.readlines()
else:
student_md = []
flag = False
if student_md:
with open(filename, 'w', encoding='utf-8')as wfile:
d = {}
for item in student_md:
d = dict(eval(item))
if d['id'] != student_id:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print(f'id为{student_id}的学生的信息已经删除')
else:
print(f'没有找到id为{student_id}的学生的信息')
else:
print('无学生信息')
break
show()
answer = input('是否继续删除?(y/n)')
if answer == 'y' or answer == 'Y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
else:
return
student_id = input('请输入你要修改的学生的id:')
with open(filename, 'w', encoding='utf-8')as wfile:
for item in student_md:
d = dict(eval(item))
if d['id'] == student_id:
print('1.\t姓名:' + d['name'])
print('2.\t英语成绩:' + str(d['english']))
print('3.\t数学成绩:' + str(d['math']))
print('4.\t语文成绩:' + str(d['chinese']))
answer = int(input('请输入你想修改的选项:(‘0’为取消)'))
while True:
if answer in [1, 2, 3, 4]:
if answer == 1:
d['name'] = input("请输入姓名:")
try:
if answer == 2:
d['english'] = int(input("请输入英语成绩:"))
if answer == 3:
d['math'] = int(input("请输入数学成绩:"))
if answer == 4:
d['chinese'] = int(input("请输入语文成绩:"))
except:
print('输入格式错误')
continue
d['scores'] = d['english'] + d['math'] + d['chinese']
wfile.write(str(d) + '\n')
print('修改成功')
break
elif answer == 0:
break
else:
print('输入有误')
break
else:
wfile.write(str(d) + '\n')
def show_student(list):
print("\tid\t\t姓名\t英语\t数学\t语文\t总成绩")
for item in list:
d = dict(item)
id = d['id']
name = d['name']
english = d['english']
math = d['math']
chinese = d['chinese']
score = d['scores']
print(f'\t{id}\t{name}\t{english}\t\t{math}\t\t{chinese}\t\t{score}')
def sort():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_list = rfile.readlines()
student_new = []
for item in student_list:
d = dict(eval(item))
student_new.append(d)
choice = input('选择排序方式:(1为正序,0为逆序)')
if choice == '1':
isup = True
elif choice == '0':
isup = False
else:
print('输入错误。')
return
print('\t1.英语')
print('\t2.数学')
print('\t3.语文')
print('\t4.总分')
mode = input('选择排序类别:')
if mode in ['1', '2', '3', '4']:
if mode == '1':
student_new.sort(key=lambda student_new: int(student_new['english']), reverse=isup)
if mode == '2':
student_new.sort(key=lambda student_new: int(student_new['math']), reverse=isup)
if mode == '3':
student_new.sort(key=lambda student_new: int(student_new['chinese']), reverse=isup)
if mode == '4':
student_new.sort(key=lambda student_new: int(student_new['scores']), reverse=isup)
show_student(student_new)
else:
print('输入错误')
return
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
num = len(student_md)
print(f'一共有{num}名学生')
return
else:
print('一共有0个学生')
return
def show():
print("\tid\t\t姓名\t英语\t数学\t语文\t总成绩")
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
i = 0
for item in student_md:
d = dict(eval(item))
id = d['id']
name = d['name']
english = d['english']
math = d['math']
chinese = d['chinese']
score = d['scores']
print(f'\t{id}\t{name}\t{english}\t\t{math}\t\t{chinese}\t\t{score}')
if __name__ == '__main__':
main()
需求分析
- 学生管理系统应该具备的功能
- 添加学生及其成绩信息
- 将学生信息保存到文件中
- 修改和删除学生的信息
- 查询某位学生的信息
- 根据学生的成绩进行排序
- 统计学生的总分
系统设计
系统功能结构
- 学生信息管理系统的7大模块
- 录入学生信息模块
- 查找学生信息模块
- 删除学生信息模块
- 修改学生信息模块
- 学生成绩排名模块
- 统计学生总人数模块
- 显示全部学生信息模块
系统树状图分析
***
系统业务流程
用户
主界面
功能菜单
功能选择
执行操作
是否继续
是
否
系统开发必备
系统开发环境
- 操作系统:win10
- Python解释器版本:Python3.8+
- 开发工具:PyCharm
- Python内置模块:os,re
主函数设计
实现主函数
编号 | 功能 |
---|
0 | 退出系统 | 1 | 录入学生信息,调用insert()函数 | 2 | 查找学生信息,调用search()函数 | 3 | 删除学生信息,调用delete()函数 | 4 | 修改学生信息,调用modify()函数 | 5 | 对学生成绩排序,调用sort()函数 | 6 | 统计学生总人数,调用total()函数 | 7 | 显示所有学生信息,调用show()函数 |
def main():
while True:
menm()
choice=int(input('请选择:'))
if choice in [0,1,2,3,4,5,6,7]:
if choice==0:
answer = input('您确定要退出系统吗?(y/n)')
if answer=='y' or answer=='Y':
print('谢谢你的使用!!!')
break
if answer=='n' or answer=='N':
continue
else:
print('你的输入有误,返回菜单')
continue
elif choice==1:
insert()
elif choice==2:
search()
elif choice==3:
delete()
elif choice==4:
modify()
elif choice==5:
sort()
elif choice==6:
total()
elif choice==7:
show()
def menm():
print("==============学生信息管理系统==============")
print("..................功能菜单..................")
print('\t\t\t\t1.录入学生信息')
print('\t\t\t\t2.查找学生信息')
print('\t\t\t\t3.删除学生信息')
print('\t\t\t\t4.修改学生信息')
print('\t\t\t\t5.\t排序')
print('\t\t\t\t6.统计学生总人数')
print('\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t0.\t退出程序')
学生维护模块设计
录入学生信息
具体实现:
1.sava(student)函数,将学生信息保存到文件中 2.insert()函数,用于录入学生信息
代码
def insert():
student_list=[]
while True:
id = input('请输入学生ID:')
if not id:
break
name = input('请输入学生姓名:')
if not name:
break
try:
english=int(input('请输入英语成绩:'))
math=int(input('请输入数学成绩:'))
chinese=int(input('请输入语文成绩:'))
except:
print('输入无效请重新输入')
continue
student = {'id':id,'name':name,'english':english,'math':math,'chinese':chinese}
student_list.append(student)
answer=input('是否继续添加学生?(y/n)')
if answer=='y' or answer=='Y':
continue
else:
break
save(student_list)
print('学生信息录入完毕.....')
def save(list):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt=open(filename,'w',encoding='utf-8')
for item in list:
stu_txt.write(str(item)+'\n')
stu_txt.close()
学生信息删除
具体实现
1.调用delete()函数,删除学生信息 2.调用show()函数,显示学生信息<在后面的代码中完成>
代码
def delete():
while True:
student_id=input('请输入删除学生的id:')
if student_id!="":
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8')as file:
student_md=file.readlines()
else:
student_md=[]
flag=False
if student_md:
with open(filename,'w',encoding='utf-8')as wfile:
d={}
for item in student_md:
d=dict(eval(item))
if d['id']!=student_id:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id为{student_id}的学生的信息已经删除')
else:
print(f'没有找到id为{student_id}的学生的信息')
else:
print('无学生信息')
break
show()
answer = input('是否继续删除?(y/n)')
if answer=='y' or answer=='Y':
continue
else:
break
学生信息修改
具体实现
1.调用modify()函数,修改学生信息 2.调用show()函数,显示学生信息<在后面的代码中完成>
代码
def modify():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
else:
return
student_id = input('请输入你要修改的学生的id:')
with open(filename, 'w', encoding='utf-8')as wfile:
for item in student_md:
d = dict(eval(item))
if d['id'] == student_id:
print('1.\t姓名:' + d['name'])
print('2.\t英语成绩:' + str(d['english']))
print('3.\t数学成绩:' + str(d['math']))
print('4.\t语文成绩:' + str(d['chinese']))
answer = int(input('请输入你想修改的选项:(‘0’为取消)'))
while True:
if answer in [1, 2, 3, 4]:
if answer == 1:
d['name'] = input("请输入姓名:")
try:
if answer == 2:
d['english'] = int(input("请输入英语成绩:"))
if answer == 3:
d['math'] = int(input("请输入数学成绩:"))
if answer == 4:
d['chinese'] = int(input("请输入语文成绩:"))
except:
print('输入格式错误')
continue
d['scores'] = d['english'] + d['math'] + d['chinese']
wfile.write(str(d) + '\n')
print('修改成功')
break
elif answer == 0:
break
else:
print('输入有误')
break
else:
wfile.write(str(d) + '\n')
查询/统计模块设计
具体实现
1.调用search()函数,根据id查看学生的信息 2.调用total()函数,统计学生的总数
代码
def search():
student_id = input('请输入你要查看的学生id:')
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
i = 0
for item in student_md:
isExist = True
d = dict(eval(item))
if d['id'] == student_id:
isExist = False
print('\tid:\t\t' + d['id'])
print('\t姓名:\t' + d['name'])
print('\t英语成绩:' + str(d['english']))
print('\t数学成绩:' + str(d['math']))
print('\t语文成绩:' + str(d['chinese']))
print('\t总成绩:' + str(d['scores']))
break
if isExist:
print(f'id为{student_id}的同学不存在')
else:
return
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_md = rfile.readlines()
num = len(student_md)
print(f'一共有{num}名学生')
return
else:
print('一共有0个学生')
return
排序模块设计
具体实现
1.调用sort()函数,实现排序功能 2.调用show_student()函数,查看排序后的列表
代码
def show_student(list):
print("\tid\t\t姓名\t英语\t数学\t语文\t总成绩")
for item in list:
d = dict(item)
id = d['id']
name = d['name']
english = d['english']
math = d['math']
chinese = d['chinese']
score = d['scores']
print(f'\t{id}\t{name}\t{english}\t\t{math}\t\t{chinese}\t\t{score}')
def sort():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as rfile:
student_list = rfile.readlines()
student_new = []
for item in student_list:
d = dict(eval(item))
student_new.append(d)
choice = input('选择排序方式:(1为正序,0为逆序)')
if choice == '1':
isup = True
elif choice == '0':
isup = False
else:
print('输入错误。')
return
print('\t1.英语')
print('\t2.数学')
print('\t3.语文')
print('\t4.总分')
mode = input('选择排序类别:')
if mode in ['1', '2', '3', '4']:
if mode == '1':
student_new.sort(key=lambda student_new: int(student_new['english']), reverse=isup)
if mode == '2':
student_new.sort(key=lambda student_new: int(student_new['math']), reverse=isup)
if mode == '3':
student_new.sort(key=lambda student_new: int(student_new['chinese']), reverse=isup)
if mode == '4':
student_new.sort(key=lambda student_new: int(student_new['scores']), reverse=isup)
show_student(student_new)
else:
print('输入错误')
return
项目打包
1.安装第三方模块
pip install PyInstaller
不存在需要等待一会 已经存在的化,有already…,如下图 2.执行打包操作
pyinstaller -F E:\MyStudy-Notes\python\project\stuSystem.py
“E:\MyStudy-Notes\python\project\stuSystem.py” 是路径
|