filename='student.txt'
import os
def menu():
print('--------------学生管理系统-----------')
print('--------------功能菜单--------------')
print('1.录入学生信息')
print('2.查找学生信息')
print('3.删除学生信息')
print('4.修改学生信息')
print('5.分数排序')
print('6.统计学生人数')
print('7.显示全部学生信息')
print('0.退出系统')
print('-------------------------------------')
def main():
while True:
menu()
choice=int(input('请选择功能:'))
if choice in [0,1,2,3,4,5,6,7]:
if choice==0:
answer=input('您确定要退出系统吗?(Y/N):')
if answer=='Y':
print('退出系统,谢谢使用')
break
else:
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 insert():
student_list=[]
while True:
id=input('请输入学号:')
if not id:
break
name=input('请输入姓名:')
if not name:
break
try:
python=int(input('请输入python成绩:'))
math=int(input('请输入高数成绩:'))
english=int(input('请输入英语成绩:'))
except:
print('成绩非整数类型,重新输入')
continue
student={'id':id,'name':name,'python':python,'math':math,'english':english}
student_list.append(student)
answer=input('是否继续添加?(Y/N):')
if answer=='Y':
continue
else:
break
save(student_list)
print('学生信息录入完毕')
def save(lst):
try:
student_txt=open(filename,'a')
except:
student_txt=open(filename,'w')
for item in lst:
student_txt.write(str(item)+'\n')
student_txt.close()
def search():
student_list=[]
while True:
id=' '
name=' '
if os.path.exists(filename):
mode=input('按照学号查找请输入1,按照姓名查找请输入2')
if mode=='1':
id=input('请输入学生学号:')
elif mode=='2':
name=input('请输入学生姓名:')
else:
print('输入有误,请重新输入')
search()
with open(filename,'r') as file:
student=file.readlines()
for item in student:
d=dict(eval(item))
if id!=' ':
if d['id']==id:
student_list.append(d)
elif name!=' ':
if d['name']==name:
student_list.append(d)
show_res(student_list)
student_list.clear()
answer=input('是否要继续查找学生信息?(Y/N)')
if answer=='Y':
continue
else:
break
else:
print('没有保存学生信息')
return
def show_res(lst):
if len(lst)==0:
print('没有查询到学生信息')
return
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('id','姓名','python成绩','高数成绩','英语成绩','总成绩'))
format_data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^14}\t{:^14}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('python'),
item.get('math'),
item.get('english'),
int(item.get('python'))+int(item.get('math'))+int(item.get('english'))
))
def delete():
while True:
student_id=input('请输入删除学生的学号:')
if student_id!=' ':
if os.path.exists(filename):
with open(filename,'r') as file:
student_massage=file.readlines()
else:
student_massage=[]
flag=False
if student_massage:
with open(filename,'w') as file:
d={}
for item in student_massage:
d=dict(eval(item))
if ['id']!=student_id:
file.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'学号{student_id}已经删除')
else:
print(f'没有找到学号为{student_id}的信息')
else:
print('无学生信息')
break
show()
answer=input('是否继续删除?(Y/N)')
if answer=='Y':
continue
else:
break
def modify():
show()
if os.path.exists(filename):
with open(filename,'r') as file:
student_massage=file.readlines()
else:
return
student_id=input('请输入要修改的学生学号:')
with open(filename,'w') as wfile:
for item in student_massage:
d=dict(eval(item))
if d['id']==student_id:
print('找到该学生信息,可以进行修改')
while True:
try:
d['name']=input('请输入姓名')
d['python']=input('输入python成绩')
d['math']=input('输入math成绩')
d['english']=input('输入english成绩')
except:
print('输入有误,请重新输入')
else:
break
wfile.write(str(d)+'\n')
print('修改成功')
else:
wfile.write(str(d)+'\n')
answer=input('是否继续修改学生信息?(Y/N)')
if answer=='Y':
modify()
def sort():
show()
if os.path.exists(filename):
with open(filename,'r') as file:
student_lst=file.readlines()
student_new=[]
for item in student_lst:
d=dict(eval(item))
student_new.append(d)
else:
return
order=input('请选择0.升序 1.降序:')
if order=='0':
order_bool=False
elif order=='1':
order_bool=True
else:
print('输入有误,请重新输入')
sort()
mode=input('请选择排序方式1.按照python成绩,2.按照高数成绩,3.按照英语成绩,4.按照总成绩:')
if mode=='1':
student_new.sort(key=lambda x:int(x['python']),reverse=order_bool)
elif mode=='2':
student_new.sort(key=lambda x: int(x['math']), reverse=order_bool)
elif mode=='3':
student_new.sort(key=lambda x: int(x['english']), reverse=order_bool)
elif mode=='4':
student_new.sort(key=lambda x: int(x['python']+int(x['math'])+int(x['english'])), reverse=order_bool)
else:
print('输入有误,请重新输入')
sort()
show_res(student_new)
def total():
if os.path.exists(filename):
with open(filename,'r') as file:
students=file.readlines()
if students:
print(f'一共有{len(students)}名学生')
else:
print('还没有录入学生信息')
else:
print('暂未保存学生信息')
def show():
student_list=[]
if os.path.exists(filename):
with open(filename,'r') as file:
students=file.readlines()
for item in students:
student_list.append(eval(item))
if student_list:
show_res(student_list)
else:
print('暂未保存数据信息')
if __name__ == '__main__':
main()
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具