标签:join else 实验楼 from __init__ 导入 result each object
#!/usr/bin/env python3 import sys from collections import Counter class Person(object): def __init__(self, grade): self.grade = grade def get_grade(self): return self.grade class Student(Person): def __init__(self, grade): Person.__init__(self, grade) def get_grade(self): x = Counter(self.grade) return "Pass: {}, Fail: {}".format(len(self.grade) - x[‘D‘], x[‘D‘]) class Teacher(Person): def __init__(self, grade): Person.__init__(self, grade) def get_grade(self): results = [] grades = Counter(self.grade).most_common(4) for key, value in grades: s = key + ":" + str(value) results.append(s) return ", ".join(results) if __name__ == ‘__main__‘: if sys.argv[1] == ‘student‘: student = Student(sys.argv[2]) print(student.get_grade()) else: teacher = Teacher(sys.argv[2]) print(teacher.get_grade())
1.if __name__ == ‘__main__‘:是程序的入口。当.py文件被直接运行时,if __name__ == ‘__main__‘之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == ‘__main__‘之下的代码块不被运行;
2.python3严格区分Tab和空格。注意!
Python3 挑战实验 -- 类和Collection(实验楼)
标签:join else 实验楼 from __init__ 导入 result each object
原文地址:https://www.cnblogs.com/hdd871532887/p/8894228.html