Lab Building - Python3 Challenge: Classes and Collections

Target

Rewrite our student_teacher.py script in the 2.3 Inheritance section of this module in Section 11, and add the function get_grade() to the Person() class. For the teacher class, this function can automatically count the scores of the students in the teacher's class and print them out in the form of A: X, B: X, C: X, D: X according to the frequency. For the student class, the function can use Pass: X, Fail: X to count its own grades (A, B, C are Pass, and if D is obtained, it is considered to be Fail).

The student_teacher.py file can be obtained by entering the following code in the Xfce terminal

wget http://labfile.oss.aliyuncs.com/courses/790/student_teacher.py

Require

  • Please put the final student_teacher.py code file in the /home/shiyanlou/Code/ path
  • The format of the final output is judged according to the first parameter teacher or student in the command line.
  • The second input parameter on the command line is the string to be counted

hint

  • import sys
  • Counter subclasses in collections
  • format() and join

Knowledge point

  • kind
  • Collection module
  • Note the final printed form

Solution

#!/usr/bin/env python3
import sys
from collections import Counter
class Person(object):
    """
    返回具有给定名称的 Person 对象
    """

    def __init__(self, name, grade):
        self.name = name
        self.grade = grade

    def get_details(self):
        """
        返回包含人名的字符串
        """
        return self.name
    def get_grade(self):
        return self.grade


class Student(Person):
    """
    返回 Student 对象,采用 name, branch, year, grade 4 个参数
    """

    def __init__(self, name, branch, year, grade):
        Person.__init__(self, name, grade)
        self.branch = branch
        self.year = year

    def get_details(self):
        """
        返回包含学生具体信息的字符串
        """
        return "{} studies {} and is in {} year.".format(self.name, self.branch, self.year)
    def get_grade(self):
        c = Counter(self.grade)
        return "Pass: {}, Fail: {}".format(c['A']+c['B']+c['C'], c['D'])


class Teacher(Person):
    """
    返回 Teacher 对象,采用字符串列表作为参数
    """
    def __init__(self, name, papers, grade):
        Person.__init__(self, name, grade)
        self.papers = papers

    def get_details(self):
        return "{} teaches {}".format(self.name, ','.join(self.papers))

    def get_grade(self):
        c = Counter(self.grade).most_common()
        s = ["{}: {}".format(x,y) for x,y in c]
        return ','.join(s)

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Wrong Parameter!")
        sys.exit(1)
    if(sys.argv[1]=="student"):
        student = Student("Kane","CSE", 2016, sys.argv[2])
        print(student.get_grade())
    else:
        teacher = Teacher('jjj',['C++'], sys.argv[2])
        print(teacher.get_grade())


result

write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325448920&siteId=291194637