要求:
用类实现学生的成绩管理,要求实现如下功能:
1.能够获取学生的对应学科成绩、总成绩、平均成绩;
2.能够获取某一个班级的某一科成绩的最高分的学生
3.能够获取某一班级的总成绩最高分的学生
算法:
基于以上要求,设计学生类和班级类2个类来分别管理学生和班级信息,学生的信息包括姓名,班级,科目以及成绩;班级的类的信息包括班级名称,班级包含的学生;
第一步:编写学生类,基于功能要求实现如下代码:
class student(object):
'''学生类'''
def __init__(self,name,class_no):
self.name = name
self.class_no = class_no
self.total_subject = {}#使用字典存储学生各科成绩
def __getitem__(self, subject):
'''使用内置函数__getitem__实现获取科目分数'''
if subject not in self.total_subject.keys():
return none
return self.total_subject[subject]
def __setitem__(self, subject, score):
'''使用内置函数__getitem__实现添加设置科目及分数功能'''
if score>0 and score<=100 and isinstance(score,int):
self.total_subject[subject] = score
else:
print('输入的分数必须为数字且0-100之间')
def get_student_name(self):
'''获取学生姓名'''
return self.name
def get_class_no(self):
'''获取学生所在班级'''
return self.class_no
def get_all_score(self):
'''获取所有学科和分数'''
return self.total_subject
def get_highest_score(self):
'''获取最高分学科和分数'''
if self.total_subject:
return sorted(self.total_subject.items(),key = lambda x:x[1],reverse = true)[0]
else:
return none
def get_avg_score(self):
'''获取平均分'''
if self.total_subject:
return round(sum(self.total_subject.values())/len(self.total_subject),2)
else:
return none
def get_total_score(self):
'''获取总分'''
return sum(self.total_subject.values())
第二步:编写班级类如下:
class class_no():
'''班级类'''
def __init__(self,class_name):
self.class_name = class_name
self.students = []
def set_class_name(self,name):
self.class_name = class_name
def get_class_name(self,name):
return self.class_name
def add_student(self,student):
'''添加班级学生,存储的数据是学生的类的实例对象'''
self.students.append(student)
def get_specific_subject_highest_score(self,subject):
'''获取某一科目的最高分和对应学生'''
max_score = -1
max_score_student = ''
if not self.students:
return none
for student_obj in self.students:
if student_obj[subject]!=none and student_obj[subject]>max_score:
max_score = student_obj[subject]
max_score_student = student_obj.get_student_name()
return max_score_student,subject,max_score
def get_total_higest_socre_student(self):
'''获取总分最高的学生和对应总分'''
max_score = -1
max_score_student = ''
if not self.students:
return none
for student_obj in self.students:
if student_obj.get_total_score() !=none and student_obj.get_total_score() >max_score:
max_score = student_obj.get_total_score()
max_score_student = student_obj.get_student_name()
return max_score_student,max_score
第三步:造测试数据
#创建测试用例
#先创建100个学生
temp = []
for i in range(100):
import random
name = ('daihao'+str(i))
class_no = random.choice(['07计本2班','07计本1班','08计本2班','06艺术1班'])
name = student(name,class_no)
name['语文'] = random.randint(60,100)
name['数学'] = random.randint(60,100)
name['英语'] = random.randint(60,100)
temp.append(name)
#根据班级创建对应的实例
seven_one = class_no('07计本1班')
seven_two = class_no('07计本2班')
eight_two = class_no('08计本2班')
six_one = class_no('06艺术1班')
for obj in temp:
if obj.get_class_no() =='07计本1班':
seven_one.add_student(obj)
elif obj.get_class_no() =='07计本2班':
seven_two.add_student(obj)
elif obj.get_class_no() =='08计本2班':
eight_two.add_student(obj)
elif obj.get_class_no() =='06艺术1班':
six_one.add_student(obj)
#打印07计本1班语文最高分
print(seven_one.get_specific_subject_highest_score('语文'))
#打印07计本2班语文最高分
print(seven_two.get_specific_subject_highest_score('语文'))
#打印08计本2班语文最高分
print(eight_two.get_specific_subject_highest_score('语文'))
#打印06艺术1班语文最高分
print(six_one.get_specific_subject_highest_score('语文'))
#打印07计本1班总分最高分
print(seven_one.get_total_higest_socre_student())
#打印07计本2班总分最高分
print(seven_two.get_total_higest_socre_student())
#打印08计本2班总分最高分
print(eight_two.get_total_higest_socre_student())
#打印06艺术1班总分最高分
print(six_one.get_total_higest_socre_student())
#打印06艺术1班所有学生成绩
for student,each_score in six_one.get_all_student_score().items():
print('-'*30+student+'-'*30)
for subject,score in each_score.items():
print(subject,score)
tips:班级类里面的student里面存的是学生的实例对象,后续方法调用需要使用学生类的实例方法调用获取数据
希望与广大网友互动??
点此进行留言吧!