码迷,mamicode.com
首页 > 编程语言 > 详细

Python3 挑战实验 -- 类和Collection(实验楼)

时间:2018-04-21 00:22:16      阅读:508      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!