5.1Stat 클래스 만들기


    우선 functions.py에 있는 함수 중 통계와 관련 있는 함수만 따로 분리해 클래스로 만듭니다.

    코드 6-26 oop1/oop1_3/statistics.py

    import math
    
    class Stat:
        def average(self, scores):
            s = 0
            for score in scores:
                s += score
            return round(s/len(scores), 1)
    
        def variance(self, scores, avrg):
            s= 0
            for score in scores:
                s += (score - avrg) ** 2
            return round(s/len(scores), 1)
    
        def std_dev(self, variance):
            return round(math.sqrt(variance), 1)
    


    Stat 클래스에 평균, 분산, 표준편차를 구하는 함수를 묶었습니다. 이 클래스는 다른 프로그램을 작성할 때 유용하게 활용할 수 있고 통계와 관련된 더 많은 기능을 추가해 확장할 수도 있습니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.