본문 바로가기

파이썬/기본

퀴즈 8

Quiz) 주어진 코드를 활용하여 부동산 프로그램을 작성하시오.

(출력 예제)
총 3대의 매물이 있습니다.
강남 아파트 매매 10억 2010년
마포 오피스텔 전세 5억 2007년
송파 빌라 월세 500/50 2000년

 

[코드]

class House:
    # 매물 초기화 : 위치, 건물 종류, 매물 종류, 가격, 준공년도
    def __init__(self, location, house_type, deal_type, price, completion_year):
        pass

    # 매물 정보 표시
    def show_detail(self):
        pass

나의 풀이

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)

b1 = House("강남", "아파트", "매매", "10억", "2010년")
b2 = House("마포", "오피스텔", "전세", "5억", "2007년")
b3 = House("송파", "빌라", "월세", "500/50", "2000년")

buildings = []
buildings.append(b1)
buildings.append(b2)
buildings.append(b3)

for i in buildings:
    i.show_detail()

스타크래프트 프로젝트에서 솔직히 많은 부분..이 이해가 가지 않았지만

퀴즈는 그렇게 어렵지는 않았던 것 같다.

근데 총[]개의 매물이 있습니다는 풀이하지 못했다..


강의 풀이

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)


houses = []
house1 = House("강남", "아파트", "매매", "10억", "2010년")
house2 = House("마포", "오피스텔", "전세", "5억", "2007년")
house3 = House("송파", "빌라", "월세", "500/50", "2000년")

houses.append(house1)
houses.append(house2)
houses.append(house3)

print("총 {0}대의 매물이 있습니다.".format(len(houses)))
for house in houses:
    house.show_detail()

다른 점

 

객체이름과 변수 이름을 제외하고는 동일하다.

근데 왜 len이 3이 나오는지는 이해가 가지 않는다.

len은 글자의 길이를 숫자로 변환하는 거 아닌가?

근데 객체에 사용하면 객체의 개수를 세주는 것인가?

'파이썬 > 기본' 카테고리의 다른 글

퀴즈 9  (0) 2021.09.16
31. 예외처리  (0) 2021.09.16
30. 스타크래프트 프로젝트  (0) 2021.09.10
29. pass, super  (0) 2021.09.10
29. 메소드 오버라이딩  (0) 2021.09.10