import os
filename ='student.txt'defmain():whileTrue:
menu()
n =int(input("*请输入菜单功能前对应的数字:"))if n ==1:
search()elif n ==2:
change()elif n ==3:
add()elif n ==4:
delete()elif n ==5:
show()elif n ==0:
flag =0print("*您确定要退出系统吗?(yes/no)")
choice =input()while flag ==0:if choice =="yes":print("\n*欢迎您的下一次使用!")return0elif choice =="no":
flag =1else:
choice =input("*请输入yes或者no:")if flag ==1:continueelse:print("\n*请输入菜单功能对应的数字,跳转到相应功能!\n")defmenu():print("\n============欢迎使用学生信息管理系统===============")print("********************主菜单*********************")print(" 1.查找学生信息")print(" 2.修改学生信息")print(" 3.添加学生信息")print(" 4.删除学生信息")print(" 5.展示所有学生信息")print(" 0.退出系统")print("*********************************************")defsearch():whileTrue:print("\n=============================================")
label =input("*请输入您想要检索的标签(id,name,home,numb):")
key =input("*请输入您想要检索的学生信息:")if os.path.exists(filename):withopen(filename,'r', encoding='utf-8')asfile:
stu_old =file.readlines()else:
stu_old =[]if stu_old:
flag =0for i in stu_old:
d =dict(eval(i))if d[label]== key:
flag = flag +1if flag ==1:print("\n=====================================================================")print("%s:%-16s %s:%-10s %s:%-10s %s:%-20s"%('id', d['id'],'name', d['name'],'home', d['home'],'numb', d['numb']))ifnot flag:print("\n*抱歉,系统没有查询到相关学生信息")else:print("=====================================================================")else:print("\n*暂无学生信息!")
ans =input("\n*是否继续查询学生信息?(yes/no)")
flag =0while flag ==0:if ans =='yes':
flag =1elif ans =='no':
flag =2else:
ans =input("*请输入yes或者no:")if flag ==1:continueelse:breakdefchange():whileTrue:
key =input("*请输入您想要修改学生的id(输入0则退出修改):")if key:if os.path.exists(filename):withopen(filename,'r', encoding='utf-8')asfile:
stu_lst =file.readlines()else:
stu_lst =[]if stu_lst:
flag =False
new_name =''for i in stu_lst:
d =dict(eval(i))if d['id']== key:
new_name = d['name']
flag =Truebreakif flag:
new_home =input("*请输入该学生户籍:")
new_numb =input("*请输入该学生手机号:")
new_d ={'id': key,'name': new_name,'home': new_home,'numb': new_numb}
wfile =open(filename,'w', encoding='utf-8')for i in stu_lst:
d =dict(eval(i))if d['id']!= key:
wfile.write(str(i))else:
wfile.write(str(new_d)+'\n')print("\n*修改完成!")
wfile.close()else:print("\n*抱歉,未查询到该学生!")else:print("\n*抱歉,未查询到该学生!")elifnot key or key =='0':break
ans =input("\n*是否要继续修改学生信息?(yes/no)")
flag =0while flag ==0:if ans =='yes':
flag =1elif ans =='no':
flag =2else:
ans =input("*请输入yes或者no:")if flag ==1:continueelse:breakdefadd():
student_list =[]whileTrue:print("\n=============================================")
id1 =input("*请输入学生的学号(结束输入请输入0):")ifnot id1 or id1 =='0':break
name =input("*请输入学生的姓名:")ifnot name:breaktry:
home =input("*请输入学生的户籍:")
numb =input("*请输入学生的手机号:")except:print("*输入格式错误,请重新输入:")continue
student ={'id': id1,'name': name,'home': home,'numb': numb}
student_list.append(student)
save(student_list)
student_list =[]print("\n*学生信息添加成功!\n")
ans =input("*是否继续添加学生信息?(yes/no)")
flag =0while flag ==0:if ans =='yes':
flag =1elif ans =='no':
flag =2else:
ans =input("*请输入yes或者no:")if flag ==1:continueelse:breakdefsave(lst):try:
stu =open(filename,'a', encoding='utf-8')except:
stu =open(filename,'w', encoding='utf-8')for i in lst:
stu.write(str(i)+'\n')
stu.close()defdelete():whileTrue:
stu_id =input("*请输入要删除学生的学号:")if stu_id:if os.path.exists(filename):withopen(filename,'r', encoding='utf-8')asfile:
stu_lst =file.readlines()else:
stu_lst =[]
flag =Falseif stu_lst:withopen(filename,'w', encoding='utf-8')as wfile:for i in stu_lst:
d =dict(eval(i))if d['id']!= stu_id:
wfile.write(str(d)+'\n')else:
flag =Trueif flag:print('\n*该学生信息已被删除!')else:print('\n*该学生信息不存在!')else:print("\n*暂无学生信息!")
ans =input("\n*是否要继续删除学生信息?(yes/no)")
flag =0while flag ==0:if ans =='yes':
flag =1elif ans =='no':
flag =2else:
ans =input("*请输入yes或者no:")if flag ==1:continueelse:break# def show():# while True:# stu_sum = 0# if os.path.exists(filename):# with open(filename, 'r', encoding='utf-8') as file:# stu_lst = file.readlines()# else:# stu_lst = []# if stu_lst:# print("\n=====================================================================")# for i in stu_lst:# stu_sum = stu_sum + 1# d = dict(eval(i))# print("%s:%-16s %s:%-10s %s:%-10s %s:%-20s" % (# 'id', d['id'], 'name', d['name'], 'home', d['home'], 'numb', d['numb']))# print("=====================================================================")# print(" 总人数:%d" % stu_sum)## else:# print("\n*暂无学生信息!")# ans = input("\n*请输入back返回主菜单,remake重新查询:")# flag = 0# while flag == 0:# if ans == 'remake':# flag = 1# elif ans == 'back':# flag = 2# else:# ans = input("*请输入back或者remake:")# if flag == 1:# continue# else:# breakdefshow():whileTrue:if os.path.exists(filename):
stu_new =[]
stu_sum =0withopen(filename,'r', encoding='utf-8')asfile:
stu_lst =file.readlines()if stu_lst:
choice =input("*请输入排序方式(0为升序,1为降序):")whileTrue:if choice =='0'or choice =='1':breakelse:
choice =input("*请输入0或者1:")if choice =='1':
flag =Trueelif choice =='0':
flag =False
stu_new =[]for i in stu_lst:
d =dict(eval(i))
stu_new.append(d)
stu_new.sort(key=lambda x:int(x['id']), reverse=flag)print("\n=====================================================================")for i in stu_new:
d =dict(i)
stu_sum = stu_sum +1print("%s:%-16s %s:%-10s %s:%-10s %s:%-20s"%('id', d['id'],'name', d['name'],'home', d['home'],'numb', d['numb']))print("=====================================================================")print(" 总人数:%d"% stu_sum)else:print("\n*抱歉,暂无学生信息!")breakelse:print("\n*抱歉,暂无学生信息!")
ans =input("\n*请输入back返回主菜单,remake重新查询:")
flag =0while flag ==0:if ans =='remake':
flag =1elif ans =='back':
flag =2else:
ans =input("*请输入back或者remake:")if flag ==1:continueelse:breakif __name__ =='__main__':
main()