20190321-用类做一个简单的学生成绩管理系统
要求:
用类实现学生的成绩管理,要求实现如下功能:
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): '''使用内置函数__setitem__实现添加设置科目及分数功能''' 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里面存的是学生的实例对象,后续方法调用需要使用学生类的实例方法调用获取数据