filename = 'student.txt'
def main():
while True:
menu()
choice = -1
try:
choice = int(input('请选择你的操作'))
except:
print('请输入正确的数字')
continue
if choice in [0,1,2,3,4,5,6,7]:
if choice == 0:
answer = input("您确定要退出吗?y/n")
if answer == 'y' or answer == 'Y' or answer == 'yes':
print('谢谢您的使用')
break
else:
continue
elif choice == 1:
insert()
elif choice == 2:
search()
elif choice == 3:
delete()
elif choice == 4:
modify()
elif choice == 5:
my_sort()
elif choice == 6:
total()
elif choice == 7:
show()
else:
print('请进行正确的操作')
# 菜单
def menu():
print("==========================学生信息管理系统==============================")
print()
print("------------------------------功能菜单---------------------------------")
print("\t\t\t\t\t\t\t\t1.录入学生信息")
print("\t\t\t\t\t\t\t\t2.查找学生信息")
print("\t\t\t\t\t\t\t\t3.删除学生信息")
print("\t\t\t\t\t\t\t\t4.修改学生信息")
print("\t\t\t\t\t\t\t\t5.排序")
print("\t\t\t\t\t\t\t\t6.统计学生人数")
print("\t\t\t\t\t\t\t\t7.显示所有学生信息")
print("\t\t\t\t\t\t\t\t0.退出")
print("---------------------------------------------------------------------")
# 录入学生信息
def insert():
student_dict = {}
f = open(filename,'r',encoding='utf-8')
data = eval(f.read())
while True:
id = input('请输入ID如(1001)')
if not id:
print('请输入正确的ID')
continue
elif len(id) != 4:
print('请输入正确的ID')
continue
try:
i = int(id)
except:
print('请输入正确的学号')
continue
# 检查ID是否重复,符合规范
try:
s = data[id]
print('您输入的学号已存在')
answer = input('1.替换 2.跳过')
if answer == '1':
pass
elif answer == '2':
print('重新录入信息')
continue
else:
print('你的操作有误,请重新录入信息')
continue
except:
pass
name = input('请输入姓名')
if not name:
continue
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_dict[id] = student
print('添加完成')
answer = input('是否继续添加y/n? : ')
if answer == 'y' or answer == 'Y' or answer == 'yes':
continue
else:
break
# 调用save()函数
save(student_dict)
print('学生信息数据保存完成')
def save(lst):
stu_txt = open(filename,'r',encoding='utf-8')
data = stu_txt.read()
stu_txt.close()
dict_z = {}
if data != '':
dict_z = eval(data)
dict_z.update(lst)
with open(filename,'w+',encoding='utf-8')as file:
file.write(str(dict_z))
# 查找学生信息
def search():
f = open(filename, 'r', encoding='utf-8')
da = f.read()
data = eval(da)
f.close()
if len(data) == 0:
print('暂时没有学生信息可查')
return
while True:
student_id = input('输入你要查询的id')
user = data.get(student_id, None)
if user:
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID','姓名','英语成绩','python成绩','java成绩','总成绩'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
print(format_data.format(user['id'],user['name'],user['english'],user['python'],user['java'],
int(user['english'])+int(user['python'])+int(user['java'])))
c = input('是否继续查询(y/n):')
if c == 'n':
print('退出查询功能')
break
else:
continue
else:
print('你查询的id不存在,请重新输入id')
continue
# 删除学生信息
def delete():
while True:
student_id = input('请输入要删除的学生ID: ')
if student_id != '':
f = open(filename,'r',encoding='utf-8')
da = f.read()
data = eval(da)
user = data.get(student_id, None)
if user:
with open(filename,'w+',encoding='utf-8') as file:
name = data.pop(student_id)
# file.truncate()
file.write(str(data))
print(name['name'],'删除完成')
else:
print('该学号不存在,请输入正确的学号')
continue
show() # 删除后显示所有成员
c = input('是否继续删除 (y/n): ')
if c == 'n':
break
else:
continue
else:
print('请输入学号')
continue
# 修改学生信息
def modify():
while True:
show() # 删除后显示所有成员
student_id = input('请输入要修改的学生ID: ')
if student_id != '':
f = open(filename, 'r', encoding='utf-8')
da = f.read()
data = eval(da)
with open(filename, 'w+', encoding='utf-8') as file:
user = data.get(student_id, None)
if user:
try:
user['name'] = input('请输入姓名')
user['english'] = input('请输入英语成绩')
user['python'] = input('请输入python成绩')
user['java'] = input('请输入java成绩')
except:
print('你的输入有误请重新输入')
data[student_id] = user
# file.truncate()
file.write(str(data))
print(user['name'], '修改完成')
c = input('是否继续修改 (y/n): ')
if c == 'n':
break
else:
continue
else:
print('该学号不存在,请输入正确的学号')
continue
else:
print('请输入学号')
continue
# 对学生成绩进行排序
def my_sort():
show()
student_new = []
with open(filename,'r',encoding='utf-8') as file:
data = eval(file.read())
for i,j in data.items():
student_new.append(j)
asc_or_desc = input('请选择(0升序排列 1 降序排列):')
asc_or_desc_bool = True
if asc_or_desc == '0':
asc_or_desc_bool = False
elif asc_or_desc == '1':
asc_or_desc_bool = True
else:
print('你的输入有误,请重新输入')
my_sort()
mode = input('请选择排序的方式(1.按英语成绩排序 2.按python成绩排序 3.按java成绩排序 4.按总成绩排序)')
if mode == '1':
student_new.sort(key=lambda x: int(x['english']),reverse=asc_or_desc_bool)
elif mode == '2':
student_new.sort(key=lambda x: int(x['python']),reverse=asc_or_desc_bool)
elif mode == '3':
student_new.sort(key=lambda x: int(x['java']),reverse=asc_or_desc_bool)
elif mode == '4':
student_new.sort(key=lambda user: int(user['english']) + int(user['python']) + int(user['java']),reverse=asc_or_desc_bool)
else:
print('请选择正确的操作方式')
my_sort()
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID', '姓名', '英语成绩', 'python成绩', 'java成绩', '总成绩'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
for user in student_new:
print(format_data.format(user['id'], user['name'], user['english'], user['python'], user['java'],
int(user['english']) + int(user['python']) + int(user['java'])))
# 统计学生总人数
def total():
with open(filename,'r',encoding='utf-8') as file:
data = eval(file.read())
print('一共有',len(data),'名学生')
input('按回车结束 ')
# 显示所有学生的信息
def show():
with open(filename, 'r', encoding='utf-8') as file:
data = eval(file.read())
format_title = '{:^6}\t{:^12}\t{:^8}\t{:^10}\t{:^10}\t{:^8}'
print(format_title.format('ID', '姓名', '英语成绩', 'python成绩', 'java成绩', '总成绩'))
format_data = '{:^6}\t{:^12}\t{:^8}\t{:^8}\t{:^8}\t{:^8}'
for i,user in data.items():
print(format_data.format(user['id'], user['name'], user['english'], user['python'], user['java'],
int(user['english']) + int(user['python']) + int(user['java'])))
main()