运行截图:
exe执行文件:
代码:(733条消息) python实现学生信息管理系统exe文件-Python文档类资源-CSDN文库
# Country : China
# Name : Joey Chao
# Date : 2023-02-03 10:26
import os
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\n')
if answer=='y' or 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 menm():
print('==================学生信息管理系统==================')
print(' 功能菜单')
print('\t\t\t\t\t1.录入学生信息')
print('\t\t\t\t\t2.查找学生信息')
print('\t\t\t\t\t3.删除学生信息')
print('\t\t\t\t\t4.修改学生信息')
print('\t\t\t\t\t5.排序')
print('\t\t\t\t\t6.统计学生总人数')
print('\t\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t\t0.退出')
def insert():
student_list=[]
while True:
id=int(input('请输入ID:'))
if not id:
break
name=input('请输入姓名:')
if not name:
break
try:
englist=int(input('请输入英语成绩:'))
math=int(input('请输入数学成绩:'))
science=int(input('请输入科学成绩:'))
except:
print('录入无效请重新输入!')
continue
student={'id':id,'name':name,'englist':englist,'math':math,'science':science}
student_list.append(student)
save(student_list)
answer = input('您继续添加学生信息吗?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
print('学生信息录入完毕!')
def save(lst):
try:
stu_txt=open(filename,'a',encoding='utf-8')
except:
stu_txt=open(filename,'w',encoding='utf-8')
for item in lst:
stu_txt.write(str(item)+'\n')
stu_txt.close()
def search():
stu_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mode=int(input('按ID查找请输入1,按姓名查找请输入2:'))
if mode==1:
id=int(input('请输入学生ID:'))
elif mode==2:
name=input('请输入学生姓名:')
else:
print('您的输入有误,请重新输入!')
search()
with open(filename, 'r', encoding='utf-8') as rfile:
stu=rfile.readlines()
for item in stu:
d=dict(eval(item))
if id!='':
if d['id']==id:
stu_query.append(d)
elif name!='':
if d['name']==name:
stu_query.append(d)
show_stu(stu_query)
stu_query.clear()
answer = input('您继续查找学生信息吗?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('为由该学生信息!')
return
def show_stu(lst):
if len(lst)==0:
print('未查到该学生信息!!!')
return
format_title='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
print(format_title.format('ID','姓名','英语成绩','数学成绩','科学成绩','总成绩'))
format_data='{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}\t'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('englist'),
item.get('math'),
item.get('science'),
int(item.get('englist')+item.get('math')+item.get('science'))
))
def delete():
while True:
stu_id=int(input('请输入学生的ID:'))
if stu_id !='':
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as file:
stu_old = file.readlines()
else:
stu_old = []
flag = False
if stu_old:
with open(filename, 'w', encoding='utf_8')as wfile:
d = {}
for item in stu_old:
d = dict(eval(item))
if d['id'] != stu_id:
wfile.write(str(d) + '\n')
else:
flag = True
if flag:
print(f'id为{stu_id}学生信息已被删除')
else:
print(f'没有找到id为{stu_id}的学生信息')
else:
print('无学生信息')
break
show()
answer = input('您是否继续删除学生信息?y/n\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:
stu_old = rfile.readlines()
else:
return
stu_id = int(input('请输入学生的ID:'))
with open(filename, 'w', encoding='utf_8')as wfile:
for item in stu_old:
d=dict(eval(item))
if d['id']==stu_id:
print('已找到该学生信息!')
while True:
try:
d['name']=input('请输入学生姓名:')
d['englist'] = int(input('请输入英语成绩:'))
d['math'] = int(input('请输入数学成绩:'))
d['science'] = int(input('请输入科学成绩:'))
except:
print('您的输入有误,请重新输入!')
else:
break
wfile.write(str(d)+'\n')
print('修改成功!')
else:
wfile.write(str(d) + '\n')
answer = input('您是否继续修改学生信息?y/n\n')
if answer == 'y' or answer == 'Y':
modify()
def sort():
show()
if os.path.exists(filename):
with open(filename, 'r', encoding='utf_8')as rfile:
stu_lst=rfile.readlines()
stu_new=[]
for item in stu_lst:
d=dict(eval(item))
stu_new.append(d)
else:
return
asc_or_dsc=input('请选择(0为升序,1为降序):')
if asc_or_dsc=='0':
asc_or_dsc_bool=True
elif asc_or_dsc=='1':
asc_or_dsc_bool=False
else:
print('您的输入有误,重新输入!')
sort()
mode=int(input('请选择(0按总成绩,1.按英语成绩,2按数学成绩,3按科学成绩):'))
if mode==0:
stu_new.sort(key=lambda x:int(x['englist']+x['math']+x['science']),reverse=asc_or_dsc_bool)
elif mode==1:
stu_new.sort(key=lambda x:int(x['englist']),reverse=asc_or_dsc_bool)
elif mode==2:
stu_new.sort(key=lambda x: int(x['math']), reverse=asc_or_dsc_bool)
elif mode==3:
stu_new.sort(key=lambda x: int(x['science']), reverse=asc_or_dsc_bool)
else:
print('您的输入有误,请重新输入!')
sort()
show_stu(stu_new)
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf_8')as rfile:
stu=rfile.readlines()
if stu:
print(f'一共有{len(stu)}名学生')
else:
print('暂未录入学生信息!')
else:
print("无文件!")
def show():
stu_lst=[]
if os.path.exists(filename):
with open(filename, 'r', encoding='utf_8')as rfile:
stu=rfile.readlines()
for item in stu:
stu_lst.append(eval(item))
if stu:
show_stu(stu_lst)
else:
print("无文件!")
if __name__ == '__main__':
main()