QUIZ) 주어진 코드를 활용하여 부동산 프로그램을 작성하시오.
(출력예제)
총 3대의 매물이 있습니다.
강남 아파트 매매 10억 2010년
마포 오피스텔 전세 5억 2007년
송파 빌라 월세 500/50 2000년
나의 코드
class House: def __init__(self, location, house_type, deal_type, price, completion_year): self.location = location self.house_type = house_type self.deal_type = deal_type self.price = price self.completion_year = completion_year print("{0} {1} {2} {3} {4}년".format(self.location, self.house_type, self.deal_type, self.price, self.completion_year))
def show_detail(number):
print("총 {0}대의 매물이 있습니다.".format(number))
class Gangnam(House):
def init(self):
House.init(self, "강남", "아파트", "매매", "10억", 2010)
class Mapo(House):
def init(self):
House.init(self, "마포", "오피스텔", "전세", "5억", 2007)
class Songpa(House):
def init(self):
House.init(self, "송파", "빌라", "월세", "500/50", 2000)
House.show_detail(3)
show_detail1 = Gangnam()
show_detail2 = Mapo()
show_detail3 = Songpa()
풀이: 부모 클래스에 함수를 2개 만들고 각각을 클래스로 만들어서 변수로 지정해 빼냈다.
이렇게 만든 이유: "총 3대의 매물이 있습니다."라는 문장을 만들기 위해 리스트를 만들어 length를 쓰고 반복문으로 돌리려고 했다. 하지만 저 문장이 앞에 나오게 어떻게 만들수 있지? 라는 강박에 해당 부분을 함수로 처리해버렸다. 😥
생각이 맞다! 쉬운 길로 가려고 하지 말고 정답을 찾아가자!
>정답
class House:
def init(self, location, house_type, deal_type, price, completion_year):
self.location = location
self.house_type = house_type
self.deal_type = deal_type
self.price = price
self.completion_year = completion_year
def show_detail(self):
print(self.location, self.house_type, self.deal_type, self.price, self.completion_year)
House_unit = []
House1 = House("강남", "아파트", "매매", "10억", "2010년")
House2 = House("마포", "오피스텔", "전세", "5억", "2007년")
House3 = House("송파", "빌라", "월세", "500/50", "2000년")
House_unit.append(House1)
House_unit.append(House2)
House_unit.append(House3)
print("총 {0}대의 매물이 있습니다.".format(len(House_unit))
for house in House_unit:
house.show_detail()