1.python文件
代码如下(示例):
import os
filename='student.txt'
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' 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:
totol()
elif choice==7:
show()
def menu():
print('=============学生信息管理系统===============')
print('-----------------功能菜单------------------')
print('\t\t\t\t\t\t1.录入学生信息')
print('\t\t\t\t\t\t2.查找学生信息')
print('\t\t\t\t\t\t3.删除学生信息')
print('\t\t\t\t\t\t4.修改学生信息')
print('\t\t\t\t\t\t5.排序')
print('\t\t\t\t\t\t6.统计学生总人数')
print('\t\t\t\t\t\t7.显示所有学生信息')
print('\t\t\t\t\t\t0.退出')
print('------------------------------------------')
def insert():
student_list=[]
while True:
id=input('请输入学生id:')
if not id:
break
name=input('请输入学生姓名:')
if not name:
break
try:
english=int(input('请输入英语成绩:'))
python=int(input('请输入python成绩:'))
java=int(input('请输入java成绩:'))
except:
print('输入异常,请重新输入!')
continue
student={'id':id,'name':name,'english':english,'python':python,'java':java}
student_list.append(student)
answer=input('是否继续添加?y/n')
if answer=='y' or answer=='Y':
continue
else:
break
save(student_list)
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():
student_query=[]
while True:
id=''
name=''
if os.path.exists(filename):
mode=input('按照id查找请输入1,按照姓名查找请输入2:')
if mode=='1':
id=input('请输入id:')
elif mode=='2':
name=input('请输入姓名:')
else:
print('输入有误,请重新输入')
search()
with open(filename,'r',encoding='utf-8') as rfile:
student=rfile.readlines()
for item in student:
# 字符串转成字典类型
d=dict(eval(item))
if id!='':
if d['id']==id:
student_query.append(d)
else:
if d['name']==name:
student_query.append(d)
show_student(student_query)
student_query.clear()
answer = input('是否继续查找?y/n')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('暂未保存学生信息')
return
def show_student(lst):
if len(lst)==0:
print('没有查询到学生信息,无数据显示')
return
format_title='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t'
print(format_title.format('ID','NAME','ENG','JAVA','PYTHON'))
format_content='{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t'
for item in lst:
print(format_content.format(item['id'],item['name'],item['english'],item['java'],item['python']))
def delete():
while True:
delid=input('请输入要删除学生的id:')
if delid!='':
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as file:
student_old=file.readlines()
else:
student_old=[]
flag=False
if student_old:
with open(filename,'w',encoding='utf-8') as wfile:
d={}
for item in student_old:
d=dict(eval(item))
if d['id']!=delid:
wfile.write(str(d)+'\n')
else:
flag=True
if flag:
print(f'id为{delid}的信息已被删除')
else:
print(f'不存在{delid}的学生信息')
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_old=rfile.readlines()
else:
return
stuid = input('请输入学生id')
with open(filename,'w',encoding='utf-8') as wfile:
for item in student_old:
d = dict(eval(item))
if d['id'] == stuid:
print('已找到,可修改!')
while True:
try:
d['id'] = stuid
d['name'] = input('请输入姓名:')
d['english'] = input('请输入英语成绩:')
d['python'] = input('请输入python成绩:')
d['java'] = input('请输入java成绩:')
except:
print('输入有误,重新输入!')
wfile.write(str(d) + '\n')
print('修改成功')
break
else:
wfile.write(str(d)+'\n')
answer = input('是否继续修改?y/n')
if answer == 'y' or answer == 'Y':
modify()
show()
def sort():
stu=[]
with open(filename,'r',encoding='utf-8') as rfile:
students=rfile.readlines()
for item in students:
stu.append(dict(eval(item)))
stu.sort(key=lambda x:int(x['java']),reverse=False)
show_student(stu)
def totol():
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
stu_txt=rfile.readlines()
if len(stu_txt)==0:
print('没有录入学生信息')
else:
print(f'一共有{len(stu_txt)}名学生')
else:
print('暂未保存学生信息')
def show():
student_list=[]
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as rfile:
stu_txt=rfile.readlines()
for item in stu_txt:
student_list.append(eval(item))
if student_list:
show_student(student_list)
else:
print('没有录入')
if __name__ == '__main__':
main()