TIL과 과제는 분리해서 작성하기로 마음먹었다.
TIL에 과제까지 다 담기에는 글이 너무 길어지고 TIL은 개념 정리 위주로 작성하고자 함이다.
요구조건
- 인스턴스를 선언할 때 가로, 세로 길이를 받을 수 있는 클래스를 선언할 것
- 인스턴스에서 사각형, 삼각형, 원의 넓이를 구하는 메소드를 생성할 것
- 원의 넓이를 계산할 때는 세로 길이는 무시하고, 가로 길이를 원의 지름이라 가정하고 계산
1) 먼저 원의 넓이를 계산하기 위해 π 값이 필요해서 math 모듈을 임포트 해줬다.
import math
2) Area라는 이름의 Class를 선언 해주고, 가로와 세로 길이를 받을 수 있도록 set_area라는 메소드를 정의했다.
class Area():
def set_area(self, w, h):
self.w = w
self.h = h
3) 사각형, 삼각형, 원의 넓이를 구하는 공식을 활용하여 각각 메소드를 작성하였다.
def square(self):
result = self.w * self.h
return result
def triangle(self):
result = self.w * self.h / 2
return result
def circle(self):
result = (self.w / 2)**2 * math.pi
return result
4) 길이가 가로10, 세로20으로 값이 주어졌을때 각 도형의 넓이를 구하는 코드는 다음과 같다.
area = Area()
area.set_area(10, 20)
print(area.square()) # 사각형의 넓이
print(area.triangle()) # 삼각형의 넓이
print(area.circle()) # 원의 넓이
# 출력 결과
200
100.0
78.53981633974483
✨ 완성된 전체 코드
import math
class Area(): # Area라는 이름의 class 선언
def set_area(self, w, h):
self.w = w
self.h = h
def square(self):
result = self.w * self.h
return result
def triangle(self):
result = self.w * self.h / 2
return result
def circle(self):
result = (self.w / 2)**2 * math.pi
return result
area = Area()
area.set_area(10, 20)
print(area.square()) # 사각형의 넓이
print(area.triangle()) # 삼각형의 넓이
print(area.circle()) # 원의 넓이
요구조건
- 설정한 숫자를 계산해줄 클래스를 선언할 것
- 메소드를 호출해서 num1, num2를 설정할 수 있도록 할 것
- 입력된 숫자의 더하기, 빼기, 곱하기, 나누기 연산 결과를 구하는 메소드를 생성할 것
1) Calc라는 이름의 class 선언해주었다. 이때 메소드를 호출해서 num1, num2를 설정할 수 있도록 하였다.
class Calc():
def set_number(self, num1, num2):
self.num1 = num1
self.num2 = num2
2) 입력된 숫자의 사칙연산 결과를 구하는 메소드를 생성했다.
def plus(self):
result = self.num1 + self.num2
return result
def minus(self):
result = self.num1 - self.num2
return result
def multiple(self):
result = self.num1 * self.num2
return result
def divide(self):
result = self.num1 / self.num2
return result
3) num1, num2에 대한 사칙연산 출력값은 다음과 같다. (num1=20, num2=10을 대입)
calc = Calc()
calc.set_number(20, 10)
print(calc.plus()) # 더한 값
print(calc.minus()) # 뺀 값
print(calc.multiple()) # 곱한 값
print(calc.divide()) # 나눈 값
# 출력 결과
30
10
200
2.0
✨ 완성된 전체 코드
class Calc(): # Calc라는 이름의 class 선언
def set_number(self, num1, num2):
self.num1 = num1
self.num2 = num2
def plus(self):
result = self.num1 + self.num2
return result
def minus(self):
result = self.num1 - self.num2
return result
def multiple(self):
result = self.num1 * self.num2
return result
def divide(self):
result = self.num1 / self.num2
return result
calc = Calc()
calc.set_number(20, 10)
print(calc.plus()) # 더한 값
print(calc.minus()) # 뺀 값
print(calc.multiple()) # 곱한 값
print(calc.divide()) # 나눈 값
요구조건
- 사용자들의 프로필을 관리할 수 있는 클래스를 선언할 것
- 메소드를 호출해서 사용자의 프로필을 설정할 수 있도록 할 것
- 사용자의 정보를 각각 출력할 수 있는 메소드를 만들 것
1) profile이라는 이름의 class 선언해주고, 사용자 정보값이 들어있는 디렉토리 메소드를 정의
class Profile:
def __init__(self):
self.profile = {
"name": "-",
"gender": "-",
"birthday": "-",
"age": "-",
"phone": "-",
"email": "-",
}
2) 사용자의 프로필을 설정할 수 있는 메소드를 작성
def set_profile(self, profile):
self.profile = profile
3) 사용자의 정보를 각각 출력할 수 있는 메소드를 작성
def get_name(self):
return self.profile["name"]
def get_gender(self):
return self.profile["gender"]
def get_birthday(self):
return self.profile["birthday"]
def get_age(self):
return self.profile["age"]
def get_phone(self):
return self.profile["phone"]
def get_email(self):
return self.profile["email"]
4) 사용자의 프로필을 설정하는 코드를 작성
profile = Profile()
profile.set_profile({
"name": "lee",
"gender": "man",
"birthday": "01/01",
"age": 32,
"phone": "01012341234",
"email": "python@sparta.com",
})
5) 입력된 사용자의 정보를 각각 출력
print(profile.get_name()) # 이름 출력
print(profile.get_gender()) # 성별 출력
print(profile.get_birthday()) # 생일 출력
print(profile.get_age()) # 나이 출력
print(profile.get_phone()) # 핸드폰번호 출력
print(profile.get_email()) # 이메일 출력
# 출력 결과
lee
man
01/01
32
01012341234
python@sparta.com
✨ 완성된 전체 코드
class Profile:
def __init__(self):
self.profile = {
"name": "-",
"gender": "-",
"birthday": "-",
"age": "-",
"phone": "-",
"email": "-",
}
def set_profile(self, profile):
self.profile = profile
def get_name(self):
return self.profile["name"]
def get_gender(self):
return self.profile["gender"]
def get_birthday(self):
return self.profile["birthday"]
def get_age(self):
return self.profile["age"]
def get_phone(self):
return self.profile["phone"]
def get_email(self):
return self.profile["email"]
profile = Profile()
profile.set_profile({
"name": "lee",
"gender": "man",
"birthday": "01/01",
"age": 32,
"phone": "01012341234",
"email": "python@sparta.com",
})
print(profile.get_name()) # 이름 출력
print(profile.get_gender()) # 성별 출력
print(profile.get_birthday()) # 생일 출력
print(profile.get_age()) # 나이 출력
print(profile.get_phone()) # 핸드폰번호 출력
print(profile.get_email()) # 이메일 출력