1. 程式人生 > >20190321-用類做一個簡單的學生成績管理系統

20190321-用類做一個簡單的學生成績管理系統

所在 one sta each 管理 core randint not div

要求:

用類實現學生的成績管理,要求實現如下功能:

  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裏面存的是學生的實例對象,後續方法調用需要使用學生類的實例方法調用獲取數據

20190321-用類做一個簡單的學生成績管理系統