#定义学生类
class Student(object):
def __init__(self, number, name, age, sex, score):
self.number = number
self.name = name
self.age = age
self.sex = sex
self.score = score
#整理并打印学生信息
def information(self):
print("学号:{},姓名:{},年龄:{},性别:{},成绩:{}".format(self.number, self.name, self.age, self.sex, self.score))
#定义班级类
class Grade(object):
def __init__(self, gradename, students, single_number):
self.gradename = gradename
self.students = students
self.single_number = single_number
#功能:添加
def add_student(self, stu):
if stu not in self.students:
self.students.append(stu.information())
print("成功添加")
#功能:展示所有学生信息
def show(self):
print(f"班级{self.gradename}的学生如下")
for stu in self.students:
stu.information()
#功能:展示特定学号的学生信息
def single(self, single_number):
for stu in self.students:
if stu.number == self.single_number:
stu.information()
#功能:展示不及格学生的信息
def low(self):
print(f"班级{self.gradename}的不及格学生如下")
for stu in self.students:
if stu.score < 60:
stu.information()
#功能:以成绩降序展示所有学生信息
def score_low_sort(self):
self.students.sort(key=lambda stu: int(stu.score), reverse=True)
self.show()
s1=Student('1003',"小明",15,"男",70)
s2=Student('1000','小华',15,'男',55)
while True:
students = [s1,s2]
g = Grade('1班', students, 0) #创建一个班级对象
choices = dict(zip([1, 2, 3], ['添加', '展示', '退出'])) #主功能选项
choice = int(input("添加按1,展示按2,退出按3:"))
#选择主功能
if choice == 1: #主功能1:添加
number = input("学号:")
name = input("姓名:")
age = int(input("年龄:"))
sex = input("性别:")
score = int(input("成绩:"))
s = Student(number, name, age, sex, score) #创建一个学生对象
students.append(s) #在students中新添该学生
print(g.add_student(s))
continue
if choice == 2: #主功能2:展示
while True:
show_choices = {0: '展示所有', 1: '特定学号', 2: '不及格', 3: '成绩降序', 4: '退出'} #副功能(展示方面)选项
show_choice = int(input("展示所有按0,特定学号按1,不及格按2,成绩降序按3,退出按4:"))
#选择副功能
if show_choice == 0: #副功能1:展示所有学生信息
print(g.show())
elif show_choice == 1: #副功能2:展示特定学号的学生信息
single_number = input("输入特定学号:")
g = Grade('1班', students, single_number)
print(g.single(g.single_number))
elif show_choice == 2: #副功能3:展示不及格学生的信息
print(g.low())
elif show_choice == 3: #副功能4:以成绩降序展示所有学生信息
print(g.score_low_sort())
elif show_choice == 4: #副功能5:退出副功能页面
break
if choice == 3: #主功能3:退出
break
有个问题就是添加功能与展示功能是独立的,新添的学生信息在展示里显示不出来,有什么解决办法?怎么优化?