#! -*- coding:utf-8 -*-
def Show_menu():
print("-" * 34)
print("| 1. 添加学生信息 |")
print("| 2. 显示所有学生信息 |")
print("| 3. 删除学生信息 |")
print("| 4. 修改学生信息 |")
print("| 5. 按学生成绩高-低显示学生信息 |")
print("| 6. 按学生成绩低-高显示学生信息 |")
print("| 7. 按学生年龄高-低显示学生信息 |")
print("| 8. 按学生年龄低-高显示学生信息 |")
print("| 9. 保存学生信息到文件(si.txt) |")
print("| 10. 从文件中读取学生数据(si.txt)|")
print("| 11. 退出程序 |")
print("-" * 34)
students = [{'ID': 1, 'Name': '张三', 'Age': 21, 'Score': 99}, {'ID': 2, 'Name': '李四', 'Age': 23, 'Score': 97},
{'ID': 7, 'Name': '王五', 'Age': 25, 'Score': 55}, {'ID': 9, 'Name': '赵柳', 'Age': 28, 'Score': 80}]
# students = []
def Add_stus():
print("\033[41;36m 开始添加学生! \033[0m")
while True:
try:
ID = int(input("请输入学号(不可重复:)"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型 \033[0m")
continue
try:
Name = input("请输入学生姓名:")
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
while True:
try:
Age = int(input("请输入年龄:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型 \033[0m")
continue
while True:
try:
Score = int(input("请输入学生成绩:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型 \033[0m")
continue
t = 0
for i in students:
if i['ID'] == ID:
t = 1
if t == 1:
print("\033[41;36m 学号重复,添加失败 \033[0m")
else:
stuInfo = {}
stuInfo['ID'] = ID
stuInfo['Name'] = Name
stuInfo['Age'] = Age
stuInfo['Score'] = Score
students.append(stuInfo)
print("\033[41;36m 添加成功 \033[0m")
def Show_stus():
print("|--------------|--------------|--------------|--------------|")
print("|" + "学号".center(12) + "|" + "姓名".center(12) + "|" + "年龄".center(12) + "|" + "成绩".center(12) + "|")
print("|--------------|--------------|--------------|--------------|")
for i in students:
print("|%s|%s|%s|%s|" % (
str(i['ID']).center(14), i['Name'].encode(encoding='GBK').center(14).decode(encoding='GBK'),
str(i['Age']).center(14), str(i['Score']).center(14)))
print("|--------------|--------------|--------------|--------------|")
def Del_stu():
print("\033[41;36m 您选择了删除学生功能! \033[0m")
while True:
try:
delID = int(input("请输入学生学号:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41:36m 请输入int类型 \033[0m")
continue
for i, v in enumerate(students):
if v['ID'] == delID:
del students[i]
print("\033[41;36m 删除成功 \033[0m")
break
else:
print("\033[41;36m 没有此学号,删除失败 \033[0m")
def Change_stu():
print("你选择了修改学生信息的功能!")
while True:
Show_stus()
print("\033[36;41m 退出请按11 \033[0m")
while True:
try:
alterID = int(input("请输入修改学生的学号:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型 \033[0m")
continue
List = []
if alterID == 11:
break
else:
while True:
for i in students:
L = i['ID']
List.append(L)
if alterID not in List:
print("\033[41;36m 没有此学号,请重新输入 \033[0m")
break
else:
while True:
print("1. 修改学号\n2. 修改姓名\n3. 修改年龄\n4. 修改成绩\n5. 退出修改")
while True:
try:
alterNum = int(input("请输入需要修改的序号:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型! \033[0m")
continue
if alterNum == 1:
while True:
try:
NewID = int(input("请输入新的ID:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型! \033[0m")
continue
while True:
for i in students:
if i['ID'] == NewID:
print("\033[41;36m 修改失败,学号重复 \033[0m")
break
elif i['ID'] == alterID:
i['ID'] = NewID
print("\033[41;36m 学号修改成功 \033[0m")
break
elif alterNum == 2:
try:
NewName = input("请输入新名字:")
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
for i in students:
if alterID == i['ID']:
i['Name'] = NewName
print("\033[41;36m 修改成功 \033[0m")
elif alterNum == 3:
while True:
try:
NewAge = int(input("请输入新年龄:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36 请输入int类型 \033[0m")
continue
for i in students:
if alterID == i['ID']:
i['Age'] = NewAge
print("\033[41;36m 修改成功 \033[0m")
elif alterNum == 4:
while True:
try:
NewScore = int(input("请输入新成绩:"))
break
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36 请输入int类型 \033[0m")
continue
for i in students:
if alterID == i['ID']:
i['Score'] = NewScore
print("\033[41;36m 修改成功 \033[0m")
elif alterNum == 5:
break
else:
print("请输入正确的序号!")
break
def Show_score_gd():
print("|--------------|--------------|--------------|--------------|")
print("|" + "学号".center(12) + "|" + "姓名".center(12) + "|" + "年龄".center(12) + "|" + "成绩".center(12) + "|")
print("|--------------|--------------|--------------|--------------|")
students_g = sorted(students,key=lambda students:students['Score'],reverse=True)
for i in students_g:
print("|%s|%s|%s|%s|" %(
str(i['ID']).center(14), i['Name'].encode(encoding='GBK').center(14).decode(encoding='GBK'),
str(i['Age']).center(14), str(i['Score']).center(14)))
print("|--------------|--------------|--------------|--------------|")
def Show_score_dg():
print("|--------------|--------------|--------------|--------------|")
print("|" + "学号".center(12) + "|" + "姓名".center(12) + "|" + "年龄".center(12) + "|" + "成绩".center(12) + "|")
print("|--------------|--------------|--------------|--------------|")
students_d = sorted(students,key=lambda students:students['Score'],reverse=False)
for i in students_d:
print("|%s|%s|%s|%s|" %(
str(i['ID']).center(14), i['Name'].encode(encoding='GBK').center(14).decode(encoding='GBK'),
str(i['Age']).center(14), str(i['Score']).center(14)))
print("|--------------|--------------|--------------|--------------|")
def Show_age_gd():
print("|--------------|--------------|--------------|--------------|")
print("|" + "学号".center(12) + "|" + "姓名".center(12) + "|" + "年龄".center(12) + "|" + "成绩".center(12) + "|")
print("|--------------|--------------|--------------|--------------|")
students_g = sorted(students,key=lambda students:students['Age'],reverse=True)
for i in students_g:
print("|%s|%s|%s|%s|" %(
str(i['ID']).center(14), i['Name'].encode(encoding='GBK').center(14).decode(encoding='GBK'),
str(i['Age']).center(14), str(i['Score']).center(14)))
print("|--------------|--------------|--------------|--------------|")
def Show_age_dg():
print("|--------------|--------------|--------------|--------------|")
print("|" + "学号".center(12) + "|" + "姓名".center(12) + "|" + "年龄".center(12) + "|" + "成绩".center(12) + "|")
print("|--------------|--------------|--------------|--------------|")
students_d = sorted(students,key=lambda students:students['Age'],reverse=False)
for i in students_d:
print("|%s|%s|%s|%s|" %(
str(i['ID']).center(14), i['Name'].encode(encoding='GBK').center(14).decode(encoding='GBK'),
str(i['Age']).center(14), str(i['Score']).center(14)))
print("|--------------|--------------|--------------|--------------|")
def Save_file():
try:
f = open('si.txt','w')
for d in students:
f.write(str(d['ID']))
f.write(',')
f.write(d['Name'])
f.write(',')
f.write(str(d['Age']))
f.write(',')
f.write(str(d['Score']))
f.write('\n')
print("\033[41;36m 文件保存成功 \033[0m")
except OSError:
print("\033[41;36m 文件写入失败!\033[0m")
def Read_file():
try:
f = open('si.txt','r+')
L = f.readlines()
print('ID','Name','Age','Score')
for i in L:
s = i.strip()
print(s)
except OSError:
print("\033[41;36m 文件读取失败!\033[0m")
while True:
Show_menu()
try:
key = int(input("请输入要操作的选项(1-11 :)"))
except KeyboardInterrupt:
print("\033[41;36 终止程序!\033[0m")
exit()
except Exception:
print("\033[41;36m 请输入int类型 \033[0m")
else:
if key == 1:
Add_stus()
elif key == 2:
Show_stus()
elif key == 3:
Del_stu()
elif key == 4:
Change_stu()
elif key == 5:
Show_score_gd()
elif key == 6:
Show_score_dg()
elif key == 7:
Show_age_gd()
elif key == 8:
Show_age_dg()
elif key == 9:
Save_file()
elif key == 10:
Read_file()
elif key == 11:
exit()
else:
print("\033[41;36m 请输入正确的序号!!! \033[0m")