더북(TheBook)

 

 실습 특수한 메서드 다루기

 

 

Step1 다음 코드는 완성된 Human 클래스입니다. 열아홉 번째 줄에서 personHuman 클래스의 인스턴스로 만든 다음 person이 두 번 걷고 한 번 먹도록 코드를 추가해 보세요.

 

01     class Human():
02     
03         def __init__(self, name, weight):
04             self.name = name
05             self.weight = weight
06         
07         def __str__(self):
08             return "{} (몸무게 {}kg)".format(self.name, self.weight)
09         
10         def eat(self):
11             self.weight += 0.1
12             print("{}가 먹어서 {}kg이 되었습니다.".format(self.name, self.weight))
13         
14         def walk(self):
15             self.weight -= 0.1
16             print("{}가 걸어서 {}kg이 되었습니다.".format(self.name, self.weight))
17     
18     # 다음 줄에서 person을 이름과 몸무게를 갖는 Human클래스의 인스턴스로 만들어 보세요.
19     person = __________
20     __________
21     __________
22     __________
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.