ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Python 문법 기초 25 - class 포함관계(2)
    Python 2022. 10. 22. 17:02

     

    예제2)

    # 냉장고에 음식 담기 - 클래스의 포함관계로 구현
    
    class Fridge:
        isOpened = False
        foods = [] # list type으로 담기
        
        def open(self):
            self.isOpened = True
            print('냉장고 문 열기')
            
        def put(self, thing):
            if self.isOpened:
                self.foods.append(thing) # 포함
                print('냉장고 안에 음식을 저장함')
                self.food_list()
            else:
                print('냉장고 문이 닫혀 있어 음식을 저장할 수 없어요')
        
        def close(self):
            self.isOpened = False
            print('냉장고 문 닫기')
            
        def food_list(self):
            for f in self.foods:
                print('-', f.irum, f.expiry_date)
            print()
            
            
    class FoodData:
        def __init__(self, irum, expiry_date):
            self.irum = irum
            self.expiry_date = expiry_date
            
    if __name__ == '__main__':
        f = Fridge()
        
        apple = FoodData('사과', '2022-10-15')
        f.put(apple)
        f.open()
        f.put(apple)
        f.close()
        print()
        cider = FoodData('칠성사이다', '2023-10-25')
        f.open()
        f.put(cider)
        f.close()
    
    
    <console>
    냉장고 문이 닫혀 있어 음식을 저장할 수 없어요
    냉장고 문 열기
    냉장고 안에 음식을 저장함
    - 사과 2022-10-15
    
    냉장고 문 닫기
    
    냉장고 문 열기
    냉장고 안에 음식을 저장함
    - 사과 2022-10-15
    - 칠성사이다 2023-10-25
    
    냉장고 문 닫기

     

    로또 뽑기

    # 크래스의 포함관계 : 로또번호 출력하기
    
    import random
    
    class LottoBall:
        def __init__(self, num):
            self.num = num
            
    class LottoMachine:
        def __init__(self):
            self.ballList = []
            for i in range(1, 46):
                self.ballList.append(LottoBall(i)) # 포함관계
                
        def selectBalls(self):
            # 볼 섞기 전 출력
            for a in range(45):
                print(self.ballList[a].num, end = ' ')
            # 볼 섞은 후 출력
            random.shuffle(self.ballList) # 섞기
            print()
            for a in range(45):
                print(self.ballList[a].num, end = ' ')
                
            return self.ballList[0:6]
                
    class LottoUi:
        def __init__(self):
            self.machine = LottoMachine() # 포함관계
            
        def playLotto(self):
            input('로또를 시작하려면 엔터키를 누르세요')
            selectedBalls = self.machine.selectBalls()
            print('\n당첨 번호')
            for ball in selectedBalls:
                print('%d '%ball.num, end = ' ')
            
    if __name__ == '__main__':
        # lo = LottoUi()
        # lo.playLotto()
        LottoUi().playLotto()
    
    
    <console>
    로또를 시작하려면 엔터키를 누르세요
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 
    37 14 12 3 32 27 9 10 36 40 42 5 26 38 17 31 1 11 8 15 25 21 35 28 18 23 33 2 39 43 24 20 45 29 4 34 6 30 22 19 44 7 16 13 41 
    당첨 번호
    37  14  12  3  32  27

     

    댓글

Designed by Tistory.