<jupyter_start><jupyter_text>13. 당신의 행운의 숫자는? 나만의 n면체 주사위 위젯 만들기**n면체 주사위를 구현하는 프로젝트를 통해 파이썬 클래스 활용법과 객체 지향 프로그래밍(OOP) 개념을 실습해 본다.**  13-1. 들어가며  13-2. 객체 지향 프로그래밍 (1) 객체란?<jupyter_code>myvar = 3
myvar
myword = 'cat'
myword
myword.upper()
'cat'.upper()
var = 4
var
print(id(var), id(4)) #id 값은 pc마다 다릅니다.
mylist = [1,2,3]
var = mylist
var
mylist.append(4)
print(mylist)
print(var)
print(id(mylist), id(var))<jupyter_output><empty_output><jupyter_text>13-3. 객체 지향 프로그래밍 (2) 객체 지향 프로그래밍이란?  13-4. 클래스 기본 문법 (1) 클래스 선언 및 인스턴스화<jupyter_code>class Car:
    pass

class Car():
    pass

#id(Car)는 여러번 호출해도 같은 값이 얻어집니다. 
print(id(Car))
print(id(Car))

#id(Car())는 Car()가 호출될 때마다 다른 값이 얻어집니다. 
print(id(Car()))
print(id(Car()))

# 두 객체의 type을 살펴봅니다. 
print(type(Car))
print(type(Car()))
mycar = Car()
mycar2 = Car()
print(id(mycar))
print(id(mycar2))<jupyter_output><empty_output><jupyter_text>13-5. 클래스 기본 문법 (2) 클래스 속성과 메서드<jupyter_code>class Car:
    color = 'red'
    category = 'sports car'

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)<jupyter_output><empty_output><jupyter_text>```python Car 클래스입니다.class Car:    '''    속성은 클래스의 상태를 나타냅니다.    색상: 빨강    종류: 스포츠 카    '''    color = 'red'    category = 'sports car'		    '''    동작은 메서드로 나타냅니다.    '''    def drive(self):        '''        주행 메서드        '''        print("I'm driving")				    def accel(self, speed_up, current_speed=10):        '''        가속 메서드        :param speed_up: 가속        :param current_speed: 현재 속도        :type speed_up: string        :type current_speed: string        '''        self.speed_up = speed_up        self.current_speed = current_speed + speed_up        print("speed up", self.speed_up, "driving at", self.current_speed)```<jupyter_code>mycar = Car()
print(mycar.color)
mycar.price
mycar.drive()
mycar.accel(5)
mycar.drive()
Car.drive(mycar)
class Test:
    def run1(self):
        print("run1")

    def run2():
        print("run2")

t = Test()
t.run1()
t.run2()
class Test2:
    def run1(self, a):
        self.a = float(a) * 10
        print(self.a)

    def run2(self, b):
        b = float(b) + 10
        print(self.b)
        
t = Test2()
t.run1(1)
t.run2(1)<jupyter_output><empty_output><jupyter_text>13-6. 클래스 기본 문법 (3) 생성자<jupyter_code>class Car:
    color = 'red'
    category = 'sports car'

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
class Car2:
    def __init__(self, color, category):
        self.color = color
        self.category = category

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
#인스턴스 객체 선언
car1 = Car()
car2 = Car2('yellow', 'sedan')
car1.color
car2.color
car1.category
car2.category
class Car2:
    def __init__(self, color='red', category='sprots car'):
        self.color = color
        self.category = category<jupyter_output><empty_output><jupyter_text>13-7. 클래스 기본 문법 (4) 클래스 변수와 인스턴스 변수<jupyter_code>class Car:
    Manufacture = "India"

    def __init__(self, color, category='sedan'):
        self.color = color
        self.category = category
car1 = Car('red','sports car')
car2 = Car('white')
print(car1.Manufacture, car1.color, car1.category)
print(car2.Manufacture, car2.color, car2.category)<jupyter_output><empty_output><jupyter_text>13-8. 클래스, 조금 더 알아보기 - 상속<jupyter_code>class Car:
    Manufacture = "India"

    def __init__(self, color='red', category='sedan'):
        self.color = color
        self.category = category

    def drive(self):
        print("I'm driving")

    def accel(self, speed_up, current_speed=10):
        self.speed_up = speed_up
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
class NewCar(Car):
    pass

car = NewCar()
car.drive()
car.accel(10)
class NewCar(Car):
    maker = 'Porsche'

car = NewCar()
car.maker
class NewCar(Car):
    def fly(self):
        print("I'm flying!! This is the new car!!")
class NewCar(Car):
    def fly(self):
        print("I'm flying!! This is the new car!!")

    def drive(self):
        print("I'm driving and can fly")
class NewCar(Car):
    def __init__(self, color, category, maker):
        super().__init__(color, category)
        self.maker = maker

    def fly(self):
        print("I'm flying!! This is the new car!!")

    def accel(self, speed_up, level=1, current_speed=10):
        self.boost[level] = {1 : 0, 2 : 30, 3 : 50}
        self.speed_up = speed_up + self.boost[level]
        self.current_speed = current_speed + self.speed_up
        print("speed up", self.speed_up, "driving at", self.current_speed)
class Car:
    Manufacture = "India"

    def __init__(self, color='red', category='sedan'):
        self.color = color 
        self.category = '2020Y '+ category


class NewCar(Car):
    def __init__(self, color, category, maker):
        super().__init__(color, category)
        self.maker = maker

newcar = NewCar('red','sports car', 'Kia')
print(newcar.category)<jupyter_output><empty_output><jupyter_text>13-9. n면체 주사위 만들기 (1) 기획  13-10. n면체 주사위 만들기 (2) 주 프로그램 - main 함수<jupyter_code>def main():
    n = get_inputs()
    mydice = FunnyDice(n)
    mydice.throw()
    print("행운의 숫자는? {}".format(mydice.getval()))<jupyter_output><empty_output><jupyter_text>13-11. n면체 주사위 만들기 (3) FunnyDice 클래스<jupyter_code>class FunnyDice:
    def __init__(self, n):
            self.n = n
    
    def throw(self):
        pass
    
    def getval(self):
        pass

    def setval(self, val):
        pass
from random import randrange

class FunnyDice:
    def __init__(self, n=6):
        self.n = int(n)
        self.numbers = list(range(1, n+1))
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]
from random import randrange

class FunnyDice:
    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]
    
    def getval(self):
        return self.val
from random import randrange

class FunnyDice:    
    def setval(self, val:int):
        if val <= self.n:
            self.val = val
        else:
            msg = "주사위에 없는 숫자입니다. 주사위는 1 ~ {0}까지 있습니다. ".format(self.n)
            raise ValueError(msg)
class FunnyDice:
    def __init__(self, n=6):
        self.n = int(n)
        self.numbers = list(range(1, n+1))
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]

    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.numbers[self.index]

    def getval(self):
        return self.val

    def setval(self, val):
        if val <= self.n:
            self.val = val
        else:
            msg = "주사위에 없는 숫자입니다. 주사위는 1 ~ {0}까지 있습니다. ".format(self.n)
            raise ValueError(msg)<jupyter_output><empty_output><jupyter_text>13-12. n면체 주사위 만들기 (4) FunnyDice 클래스 테스트, get_input 함수<jupyter_code># 코드를 입력하세요.
lucknum = FunnyDice()
lucknum.throw()
print(lucknum.getval())
lucknum.setval(7)<jupyter_output><empty_output><jupyter_text>```python 정답 코드lucknum = FunnyDice()lucknum.throw()print(lucknum.getval())lucknum.setval(7)```<jupyter_code>def get_inputs():
    n = int(input("주사위 면의 개수를 입력하세요: "))
    return n<jupyter_output><empty_output><jupyter_text>13-13. n면체 주사위 만들기 (5) 완성 코드<jupyter_code># funnydice.py

from random import randrange

class FunnyDice:
    def __init__(self, n=6):
        self.n = n
        self.options = list(range(1, n+1))
        self.index = randrange(0, self.n)
        self.val = self.options[self.index]
    
    def throw(self):
        self.index = randrange(0, self.n)
        self.val = self.options[self.index]
    
    def getval(self):
        return self.val
    
    def setval(self, val):
        if val <= self.n:
            self.val = val
        else:
            msg = "주사위에 없는 숫자입니다. 주사위는 1 ~ {0}까지 있습니다. ".format(self.n)
            raise ValueError(msg)

def get_inputs():
    n = int(input("주사위 면의 개수를 입력하세요: "))
    return n

def main():
    n = get_inputs()
    mydice = FunnyDice(n)
    mydice.throw()
    print("행운의 숫자는? {0}".format(mydice.getval()))

if __name__ == '__main__':
    main()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1379508385021824, 'fraction_numerical': 0.01745922352400643, 'mean_word_length': 2.859485815602837, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '219519', 'license': 'MIT', 'n_tokens_mistral': 3679, 'n_tokens_neox': 3953, 'n_words': 756, 'path': 'FUNDAMENTALS/Node_13/[F-13] Only_LMS_Code_Blocks.ipynb', 'repo_name': 'HRPzz/AIFFEL', 'size': '8706'}