import os
import ast
a='student.txt'
def menm():
s1 = '学生信息管理系统'
s2 = '功能菜单'
print(s1.center(40, '='))
print(s2.center(42, '-'))
print('1.录入学生信息'.rjust(24))
print('2.查找学生信息'.rjust(24))
print('3.删除学生信息'.rjust(24))
print('4.修改学生信息'.rjust(24))
print('5.排序'.rjust(20))
print('6.统计学生总人数'.rjust(25))
print('7.显示所有学生信息'.rjust(26))
print('0.退出系统'.rjust(22))
print('---------------------------------------------')
print()
def main():
while True :
menm()
x=int(input('请选择:'))
if x==0:
y=input('确定要退出吗y/n')
if y=='Y' or y=='y':
print('谢谢使用')
break
else:
continue
elif x==1:
insert()
elif x==2:
search()
elif x==3:
delete()
elif x==4:
modify()
elif x==5:
sorta()
elif x==6:
total()
elif x==7:
show()
input('继续')
def insert():
student_list=[]
while True:
id=input('请输入学生学号:')
if not id:
break
with open(a,'r',encoding='utf-8') as file2:
student_old=file2.readlines()
flag=False
if student_old:
d={}
for item in student_old:
d=ast.literal_eval(item)
if d['id'] == id:
flag=True
if flag:
print('学号存在')
break
name = input('请输入学生姓名:')
if not name:
break
try:
english=int(input('请输入英语成绩:'))
java=int(input('请输入java成绩:'))
python=int(input('请输入python成绩:'))
except:
print('输入无效重新输入:')
continue
student={'id':id,'name':name,'english':english,'java':java,'python':python}
student_list.append(student)
save(student_list)
answer=input('是否继续添加y/n')
print('')
if answer =='y':
continue
else:
break
print("学生信息录入完毕!!!")
def save(lis):
file=open(a,'a',encoding='utf-8')
for item in lis:
file.write(str(item)+'\n')
file.close()
def search():
student_query=[]
while True:
id=''
name=''
if os.path.exists(a):
mode=input('按id查找输入1,按姓名查找输入2')
if mode=='1':
id=input('请输入学生id:')
elif mode =='2':
name=input('请输入学生姓名:')
else:
print('输入有误,重新输入')
continue
with open(a,'r',encoding='utf-8') as file:
student=file.readlines()
for item in student:
d=ast.literal_eval(item)
if id!='':
if d['id']==id:
student_query.append(d)
elif name!='':
if d['name']==name:
student_query.append(d)
show_student(student_query)
student_query.clear()
anwser=input('是否继续查找学生信息"y/n\n')
if anwser=='y' or anwser=='Y':
continue
else:
break
else :
print('没有学生信息')
return
def show_student(lis):
if len(lis)==0:
print('没有查到该学生的信息')
return
else:
title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(title.format('id','姓名','英语','java','python','总分' ))
data='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
for item in lis:
print(data.format(item['id'],item['name'],item['english'],item['java'],item['python'],int(item['english']+item['java']+item['python'])))
print('')
def delete():
while True:
student_id=input('请输入要删除的学生id:')
if student_id!='':
if os.path.exists(a):
with open(a,'r',encoding='utf-8') as file1:
student_old=file1.readlines()
else :
student_old=[]
flag=False
if student_old:
with open(a,'w',encoding='utf-8') as wfile:
d={}
for item in student_old:
d=ast.literal_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()
falg=False
if os.path.exists(a):
with open(a,'r',encoding='utf-8') as file1:
student_old=file1.readlines()
else:
return
student_id=input("请输入要修改学生的id:")
d={}
with open(a,'w',encoding='utf-8') as wfile:
for item in student_old:
d=ast.literal_eval(item)
if d['id']==student_id:
print('找到此学生了,可以修改')
while True:
try:
d['name']=input('请输入学生姓名:')
d['english']=int(input('请输入英语成绩:'))
d['java']=int(input('请输入java成绩:'))
d['python']=int(input('请输入python成绩:'))
except:
print('输入错误,请重新输入')
else:
break
wfile.write(str(d)+'\n')
print('修改成功')
else:
wfile.write(str(d)+'\n')
falg=True
if falg:
print('没有找到修改的学生的信息')
answer=input('是否继续修改其他学生的信息?y/n')
if answer=='y' or answer=='Y':
modify()
def sorta():
show()
student_query=[]
if os.path.exists(a):
with open(a,'r',encoding='utf-8') as file:
student=file.readlines()
for item in student:
d=ast.literal_eval(item)
student_query.append(d)
nod=int(input('请选择排序方式:1.升序 2.降序'))
if nod==1:
mod=False
elif nod==2:
mod=True
else:
print('输入错误')
sorta()
x=int(input('请选择排序科目:1.按english排序 2.按java排序 3.按python排序 0.按总成绩排序'))
if x == 1:
student_query.sort(key=lambda y: int(y['english']), reverse=mod)
elif x == 2:
student_query.sort(key=lambda y: int(y['java']), reverse=mod)
elif x == 3:
student_query.sort(key=lambda y: int(y['python']), reverse=mod)
elif x == 0:
student_query.sort(key=lambda y: int(y['english'])+int(y['java'])+int(y['python']), reverse=mod)
else:
print('输入错误')
sorta()
show_student(student_query)
else:
print('没有学生信息')
return
def total():
student_squre=[]
if os.path.exists(a):
with open(a,'r',encoding='utf-8') as file:
student_squre=file.readlines()
i=0
for item in student_squre:
i+=1
if i==0:
print('没有学生信息')
return
else:
print('学生总人数'.center(45,'*'))
print(str(i).center(45))
else:
print('没有学生信息')
return
def show ():
student_query = []
if os.path.exists(a):
with open(a,'r',encoding='utf-8') as file:
student=file.readlines()
d={}
for item in student:
d=ast.literal_eval(item)
student_query.append(d)
show_student(student_query)
else:
print('没有学生信息')
return
if __name__=='__main__':
main()