实验目的:
改写之前的脚本,实现教师及学生的得分情况
具体目标:
1、对于教师类,函数可以自动统计出老师班上学生的得分情况并按照频率的高低以的形式打印出来
2、对于学生类,函数则可以以来统计自己的成绩情况(A,B,C 为 Pass, 如果得了 D 就认为是 Fail)。
实现代码:
#!/usr/bin/env python3import collectionsimport reimport sysclass Person(object): """ 返回具有给定名称的 Person 对象 """ def __init__(self, name): self.name = name def get_details(self): """ 返回包含人名的字符串 """ return self.name def get_grade(self): return 0class Student(Person): """ 返回 Student 对象,采用 name, branch, year 3 个参数 """ def __init__(self, name, branch, year, grade): Person.__init__(self, name) self.branch = branch self.year = year self.grade = grade def get_details(self): """ 返回包含学生具体信息的字符串 """ return "{} studies {} and is in {} year.".format(self.name, self.branch, self.year) def get_grade(self): P = re.sub(r"[ABC]", "Pass ", self.grade) Q = re.sub(r"D", "Fail ", P) list = Q.split(" ") result = collections.Counter(list).most_common(2) for k, v in result: print(k + ":" + str(v), end=", ")class Teacher(Person): """ 返回 Teacher 对象,采用字符串列表作为参数 """ def __init__(self, name, papers, grade): Person.__init__(self, name) self.papers = papers self.grade = grade def get_details(self): return "{} teaches {}".format(self.name, ','.join(self.papers)) def get_grade(self): student_scores = collections.Counter(self.grade).most_common(4) for k,v in student_scores: print("{}:{}".format(k,v),end = ", ")person1 = Person('Sachin')student1 = Student('Kushal', 'CSE', 2005, sys.argv[2])teacher1 = Teacher('Prashad', ['C', 'C++'],sys.argv[2])print(person1.get_details())if sys.argv[1] == "student": print(student1.get_details()) print(student1.get_grade())else: print(teacher1.get_details()) print(teacher1.get_grade())运行结果:
|
|