运行效果:
源码:
import os
# 文件名字
filename = 'Stu_manger.txt'
def main():
while True:
menu()
choice = int(input('请选择:'))
if choice in range(0, 8):
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:
total()
elif choice == 7:
show()
pass
def menu():
print("*******************************学生信息管理系统*******************************")
print('---------------------------------功能菜单---------------------------------')
print('1.录入学生信息')
print('2.查找学生信息')
print('3.删除学生信息')
print('4.修改学生信息')
print('5.排序')
print('6.统计学生总人数')
print('7.显示所有学生信息')
print('0.退出系统')
print('--------------------------------------------------------------------------')
pass
def insert():
stu_list = []
while True:
id = input('请输入ID(如1001):')
if not id:
break
name = input('请输入姓名:')
if not name:
break
try:
math = int(input('请输入高数成绩:'))
english = int(input('请输入英语成绩:'))
python = int(input('请输入Python成绩:'))
except:
print('输入类型有误,请重新输入!')
continue
# 将录入的学生信息封装到字典中
student = {
'id': id,
'name': name,
'math': math,
'english': english,
'python': python
}
# 将学生信息添加到列表中
stu_list.append(student)
answer = input('是否继续添加?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
# 保存学生信息到文档中
save(stu_list)
print('学生信息录入完毕!')
def save(lst):
# 检测文件是否已创建,创建则附加信息,否则新建文件
try:
fp = open(filename, 'a', encoding='utf-8')
except:
fp = open(filename, 'w', encoding='utf-8')
# 信息逐条存储
for item in lst:
fp.write(str(item) + '\n')
fp.close()
def search():
stu_query = []
while True:
id = ''
name = ''
if os.path.exists(filename):
mode = input('1.按ID查找,2.按姓名查找.')
if mode == '1':
id = input('请输入学生ID:')
elif mode == '2':
name = input('请输入学生姓名:')
else:
print('您的输入有误,请重新输入!')
search()
with open(filename, 'r', encoding='utf-8') as fp:
stu_msg = fp.readlines()
for item in stu_msg:
dic = dict(eval(item))
if id != '':
if dic['id'] == id:
stu_query.append(dic)
elif name != '':
if dic['name'] == name:
stu_query.append(dic)
# 显示查询结果
show_student(stu_query)
# 清空列表
stu_query.clear()
answer = input('是否要继续查询?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
else:
print('暂未保存学生信息!')
return
pass
def show_student(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{:^18}\t{:^10}'
for item in lst:
print(format_data.format(item.get('id'),
item.get('name'),
item.get('math'),
item.get('english'),
item.get('python'),
int(item.get('math')) + int(item.get('english')) + int(item.get('python'))
))
def delete():
"""
输入要删除的学生ID,将已存储的学生信息文件读取到一个字典变量中,
再遍历字典中是否存在该学生ID,若存在则删除!
:return:
"""
# 标记是否删除
flag = False
# 获取已存储信息
dic = {}
while True:
del_id = input('请输入要删除的学生ID:')
if del_id != '':
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8')as fp:
stu_msg = fp.readlines()
else:
stu_msg = []
if stu_msg:
with open(filename, 'w', encoding='utf-8') as fp:
for item in stu_msg:
dic = dict(eval(item)) # 将字符串转成字典
if dic['id'] != del_id:
fp.write(str(dic) + '\n')
else:
flag = True
if flag:
print(f'ID为{del_id}的学生信息已删除!')
else:
print(f'没有找到ID为{del_id}的学生信息')
else:
print('无学生信息!')
break
show()
answer = input('是否继续删除?y/n\n')
if answer == 'y' or answer == 'Y':
continue
else:
break
def modify():
"""
先将已存储的学生信息文件读取到一个字典变量中,后输入要修改的学生ID
再逐个遍历字典中是否存在该学生ID,若存在则进行修改!
:return:
"""
show()
# 判断文件是否存在,存在则读取已存储的学生信息
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as fp:
stu_msg = fp.readlines()
else:
return
mod_id = input('请输入要修改的学生ID:')
with open(filename, 'w', encoding='utf-8') as fp:
for item in stu_msg:
dic = dict(eval(item))
if dic['id'] == mod_id:
print('找到该学生的信息,可以进行修改相关信息!')
while True:
try:
dic['name'] = input('请输入姓名:')
dic['math'] = int(input('请输入高数成绩:'))
dic['english'] = int(input('请输入英语成绩:'))
dic['python'] = int(input('请输入Python成绩:'))
break
except:
print('您的输入有误,请重新输入!')
fp.write(str(dic) + '\n')
print('修改成功!')
else:
fp.write(str(dic) + '\n')
answer = input('是否继续修改其他学生信息?y/n\n')
if answer == 'y' or answer == 'Y':
modify()
else:
return
def sort():
show()
if os.path.exists(filename):
with open(filename,'r',encoding='utf-8') as fp:
stu_list = fp.readlines()
stu_new = []
for item in stu_list:
dic = dict(eval(item))
stu_new.append(dic)
else:
return
asc_or_desc = input('请选择(0.升序,1.降序):')
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('您的输入有误,请重新输入!')
sort()
mode = input('请选择排序方式(1.按高数成绩排序 2.按英语成绩排序 3.按Python成绩排序 0.按总成绩排序')
if mode == '1':
stu_new.sort(key=lambda x:int(x['math']),reverse=asc_or_desc_bool)
pass
elif mode == '2':
stu_new.sort(key=lambda x:int(x['english']),reverse=asc_or_desc_bool)
pass
elif mode == '3':
stu_new.sort(key=lambda x:int(x['python']),reverse=asc_or_desc_bool)
pass
elif mode == '0':
stu_new.sort(key=lambda x:int(x['math'])+int(x['english'])+int(x['python']),reverse=asc_or_desc_bool)
pass
else:
print('您的输入有误,请重新输入.')
sort()
show_student(stu_new)
def total():
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as fp:
stu_msg = fp.readlines()
if stu_msg:
print(f'一共有{len(stu_msg)}名学生.')
else:
print('暂时没有录入学生信息.')
else:
print('暂未保存数据信息.')
def show():
stu_list = []
if os.path.exists(filename):
with open(filename, 'r', encoding='utf-8') as fp:
stu_msg = fp.readlines()
for item in stu_msg:
stu_list.append(eval(item))
if stu_list:
show_student(stu_list)
else:
print('暂未保存数据信息.')
main()