声明:代码部分借鉴:https://blog.csdn.net/suu_an/article/details/108525293
本文在原文基础上增加了更多功能以及对非法输入的处理。发表此文,仅仅是进行自我记录。
原代码:
# 创建学生单个类
class Student:
def __init__(self,sid,sname,sfox,sc,senglish):
self.__sid = sid
self.__sname = sname
self.__sfox = sfox
self.__sc = sc
self.__senglish = senglish
def __str__(self):
return f"{self.__sid}\t{self.__sname}\t{self.__sfox}\t{self.__sc}\t{self.__senglish}"
# 封装方法
def getSid(self):
return self.__sid
def setSid(self,sid):
self.__sid = sid
def getSname(self):
return self.__sname
def setSname(self,sname):
self.__sname = sname
def getSfox(self):
return self.__sfox
def setSfox(self,sfox):
self.__sfox = sfox
def getSc(self):
return self.__sc
def setSc(self,sc):
self.__sc = sc
def getSenglish(self):
return self.__senglish
def setSenglish(self,senglish):
self.__senglish = senglish
# 创建学生集合类
class School():
#创建学生容器
stus = []
print("---进入学生管理系统---")
print("---1.学生信息添加---")
print("---2.学生信息插入---")
print("---3.学生信息修改---")
print("---4.学生信息查询---")
print("---5.学生信息删除---")
print("---6.学生信息遍历---")
print("---7.学生总分统计---")
print("---8.学生总分降序排序---")
print("---0.退出系统---")
def menu(self):
num = input("请输入要选择的功能:")
while not num.isdigit():
num = input("请规范输入数字:")
num = int(num)
if num == 1:
self.addStu()
elif num == 2:
self.insertStu()
elif num == 3:
self.modifyStu()
elif num == 4:
self.findStu()
elif num == 5:
self.delStu()
elif num == 6:
self.showallStu()
elif num == 7:
self.sumStu()
elif num == 8:
self.sortStu()
elif num == 0:
print("---退出学生管理系统---")
exit()
else:
print("输入错误,请输入0~8之间的数字")
self.menu()
def key(self):
s = input("输入Y(y)继续,输入N(n)返回主界面:")
while ((s != "Y") and (s != "y") and (s != "N") and (s != "n")):
s = input("请规范输入Y(y)继续,N(n)返回主界面:")
if s == "Y" or s == "y":
return True
else:
s == "N" or s == "n"
return False
# 学生信息缓存
def cacheStu(self,n):
sno = input('请输入学生学号:')
while not sno.isdigit():
sno = input("请规范输入学号:")
sno = int(sno)
for i in self.stus:
if sno == i.getSid():
print("您输入的学号信息已存在,请重新输入:")
if n==1:
self.addStu()
else:
self.insertStu()
sname = input("请输入学生姓名:")
sfox = input("请输入学生FOX成绩(整数或小数):")
while not sfox.isdigit():
try:
sfox = float(sfox)
break
except:
print('输入有误')
sfox = input("请规范输入学生FOX成绩:")
sfox = float(sfox)
sc = input("请输入学生C成绩(整数或小数):")
while not sc.isdigit():
try:
sc = float(sc)
break
except:
print('输入有误')
sc = input("请规范输入学生C成绩:")
sc = float(sc)
senglish = input("请输入学生ENGLISH成绩(整数或小数):")
while not senglish.isdigit():
try:
senglish = float(senglish)
break
except:
print('输入有误')
senglish = input("请规范输入学生ENGLISH成绩:")
senglish = float(senglish)
s = Student(sno,sname,sfox,sc,senglish)
return s
# 学生信息添加
def addStu(self):
s = self.cacheStu(1)
self.stus.append(s)
print("学生信息添加成功")
print("学号:"+str(s.getSid())+" 姓名:"+s.getSname()+" FOX成绩: "+str(s.getSfox())+" C成绩:"+str(s.getSc())+" ENGLISH成绩:"+str(s.getSenglish()))
key_c = self.key()
if key_c:
self.addStu()
else:
print("------------------")
self.menu()
# 学生信息插入
def insertStu(self):
n = input("请输入需要插入的位置(输入大于/等于1的整数):")
while not (n.isdigit() and int(n)>0):
n = input("请规范输入数字:")
n = int(n)
s = self.cacheStu(2)
self.stus.insert(n-1,s)
print("学生信息添加成功")
print("学号:"+str(s.getSid())+" 姓名:"+s.getSname()+" FOX成绩: "+str(s.getSfox())+" C成绩:"+str(s.getSc())+" ENGLISH成绩:"+str(s.getSenglish()))
key_c = self.key()
if key_c:
self.insertStu()
else:
print("------------------")
self.menu()
# 学生信息修改
def modifyStu(self):
sno = input('请输入要修改的学生学号:')
while not sno.isdigit():
sno = input("请规范输入学号:")
sno = int(sno)
flag = False
for i in self.stus:
if sno == i.getSid():
flag = True
break
if flag :
print(f"学号:{i.getSid()},姓名:{i.getSname()},FOX成绩:{i.getSfox()},C成绩:{i.getSc()},ENGLISH成绩:{i.getSenglish()}")
while 1:
sno = input('请输入修改后的学生学号:')
while not sno.isdigit():
sno = input("请规范输入学号:")
sno = int(sno)
for j in self.stus: #判断修改后的学号是否与(除开要修改的)其他学号重合(学号应唯一)
if j.getSid() == sno and j.getSid() != i.getSid():
print("与其他学号重合,请重新输入")
break #跳出for循环
if j.getSid() == sno and j.getSid() != i.getSid():
continue #不执行下面语句,直接回到while 1
break #跳出while 1语句
i.setSid(sno)
sname = input("请输入您要修改的姓名:")
i.setSname(sname)
sfox = input("请输入您要修改的FOX成绩:")
while not sfox.isdigit():
try:
sfox = float(sfox)
break
except:
print('输入有误')
sfox = input("请规范输入学生FOX成绩:")
sfox = float(sfox)
i.setSfox(sfox)
sc = input("请输入您要修改的C成绩:")
while not sc.isdigit():
try:
sc = float(sc)
break
except:
print('输入有误')
sc = input("请规范输入学生C成绩:")
sc = float(sc)
i.setSc(sc)
senglish = input("请输入您要修改的ENGLISH成绩:")
while not senglish.isdigit():
try:
senglish = float(senglish)
break
except:
print('输入有误')
senglish = input("请规范输入学生ENGLISH成绩:")
senglish = float(senglish)
i.setSenglish(senglish)
print("---学生信息修改完成---")
print(f"学号:{i.getSid()},姓名:{i.getSname()},FOX成绩:{i.getSfox()},C成绩:{i.getSc()},ENGLISH成绩:{i.getSenglish()}")
else:
print("学生信息不存在,请重新输入:")
self.modifyStu()
key_c = self.key()
if key_c:
self.modifyStu()
else:
print("------------------")
self.menu()
# 学生信息查询
def findStu(self):
sno = input("请输入要查询的学生学号:")
while not sno.isdigit():
sno = input("请规范输入查询学号:")
sno = int(sno)
flag = False
for k in self.stus:
if sno == k.getSid():
print("学生信息查询完成")
flag = True
break
if flag:
print("学号:"+str(k.getSid())+" 姓名:"+k.getSname()+" FOX成绩: "+str(k.getSfox())+" C成绩:"+str(k.getSc())+" ENGLISH成绩:"+str(k.getSenglish()))
else:
print("学生信息不存在,请重新输入学号")
self.findStu()
key_c = self.key()
if key_c:
self.findStu()
else:
print("------------------")
self.menu()
# 学生信息删除
def delStu(self):
sno = input("请输入要删除的学生学号:")
while not sno.isdigit():
sno = input("请规范输入删除学号:")
sno = int(sno)
t = 0
try:
for i in self.stus:
if sno == i.getSid():
break
else:
t += 1
print("---学生信息删除中---")
print("--------------------")
self.stus.pop(t)
print("---学生信息删除成功---")
except:
print('无该学号信息或已被删除')
print("请重新输入")
self.delStu()
key_c = self.key()
if key_c:
self.findStu()
else:
print("------------------")
self.menu()
# 学生信息遍历
def showallStu(self):
print("学生信息的遍历:")
for i in self.stus:
print(i)
print("------------------")
self.menu()
# 学生总分计算
def sumStu(self):
print("学生总分:")
for i in self.stus:
sum1 = float(i.getSfox()) + float(i.getSc()) + float(i.getSenglish())
print("学号:"+str(i.getSid())+"姓名:"+str(i.getSname())+"总成绩:"+str(sum1))
print("------------------")
self.menu()
# 学生总分降序排序
def sortStu(self):
score = [] #暂时储存学生总分
for i in self.stus:
sum1 = float(i.getSfox()) + float(i.getSc()) + float(i.getSenglish())
score.append(sum1)
print("降序排序:")
score.sort(reverse=True) #reverse默认为False升序
for j in score:
for k in self.stus:
sum1 = float(k.getSfox()) + float(k.getSc()) + float(k.getSenglish())
if sum1 == j:
break;
print("学号:"+str(k.getSid())+"姓名:"+str(k.getSname())+"总成绩:"+str(j))
print("------------------")
self.menu()
sc1 = School() #将School类的属性给scl
sc1.menu() #访问学生管理程序菜单
部分运行截图: