[파이썬 원격 강의 과제]220914 #4 파이썬 심화 문법 사용해보기
2022. 9. 21. 11:17ㆍ스파르타코딩클럽[AI트랙 3기]/파이썬 과제

>내가짠 코드
class Calc():
def set_number(self, a, b):
self.a = a
self.b = b
def plus(self):
result = self.a + self.b
return result
def minus(self):
result = self.a - self.b
return result
def multiple(self):
result = self.a * self.b
return result
"""
def divide(self):
try :
result = self.a / self.b
return result
except :
if self.b == 0:
print("0으로 나눌 수 없습니다.")
if self.a and self.b != int():
print("숫자만 입력 가능합니다.")
"""
calc = Calc()
calc.set_number(20, 10)
print(calc.plus())
print(calc.minus())
print(calc.multiple())
print(calc.divide())
함수안에 try, except를 넣어줌. IndentationError: expected an indented block 라는 에러 등장.
>답안 코드
class Calc():
def set_number(self, a, b):
self.a = a
self.b = b
def plus(self):
result = self.a + self.b
return result
def minus(self):
result = self.a - self.b
return result
def multiple(self):
result = self.a * self.b
return result
def divide(self):
try :
return self.a / self.b
except ZeroDivisionError:
print("0으로 나눌 수 없습니다.")
while True: #와일문 없으면 try들어가면서 숫자만 입력가능합니다 출력된 후에 밑으로 들어가줄 a,b 가 없어짐.
try:
a, b = [int(x) for x in input().split(" ")]
break #정상적으로 입력시에만 브레이크걸어서 와일문을 탈출한다.
except ValueError:
print("숫자만 입력 가능 합니다.")
calc = Calc()
calc.set_number(a, b)
print(calc.plus())
print(calc.minus())
print(calc.multiple())
print(calc.divide())
▶try를 계산기 전체에 넣지 않도록 주의해야함. 오류가 어디서 났는지 확인하기 어려워짐.
▶while문 써서 입력값 넣어준 이유는 저것만하면 a,b가 밑에 calc.set_number(a,b)까지 안넘어감. 제대로 입력할때까지 돌려주고 정상 입력하면 브레이크 걸어서 와일문 탈출해야함.


>내가짠 코드
people = [
("Blake Howell", "Jamaica", 18, "aw@jul.bw"),
("Peter Bowen", "Burundi", 30, "vinaf@rilkov.il"),
("Winnie Hall", "Palestinian Territories", 22, "moci@pacivhe.net"),
("Alfred Schwartz", "Syria", 29, "ic@tolseuc.pr"),
("Carrie Palmer", "Mauritius", 28, "fenlofi@tor.aq"),
("Rose Tyler", "Martinique", 17, "as@forebjab.et"),
("Katharine Little", "Anguilla", 29, "am@kifez.et"),
("Brent Peterson", "Svalbard & Jan Mayen", 22, "le@wekciga.lr"),
("Lydia Thornton", "Puerto Rico", 19, "lefvoru@itbewuk.at"),
("Richard Newton", "Pitcairn Islands", 17, "da@lasowiwa.su"),
("Eric Townsend", "Svalbard & Jan Mayen", 22, "jijer@cipzo.gp"),
("Trevor Hines", "Dominican Republic", 15, "ev@hivew.tm"),
("Inez Little", "Namibia", 26, "meewi@mirha.ye"),
("Lloyd Aguilar", "Swaziland", 16, "oza@emneme.bb"),
("Erik Lane", "Turkey", 30, "efumazza@va.hn"),
]
def is_adult():
if people[2] > 20 :
return True
for adult in filter(is_adult(),people):
print(adult)
print(people)
>답안
people = [
("Blake Howell", "Jamaica", 18, "aw@jul.bw"),
("Peter Bowen", "Burundi", 30, "vinaf@rilkov.il"),
("Winnie Hall", "Palestinian Territories", 22, "moci@pacivhe.net"),
("Alfred Schwartz", "Syria", 29, "ic@tolseuc.pr"),
("Carrie Palmer", "Mauritius", 28, "fenlofi@tor.aq"),
("Rose Tyler", "Martinique", 17, "as@forebjab.et"),
("Katharine Little", "Anguilla", 29, "am@kifez.et"),
("Brent Peterson", "Svalbard & Jan Mayen", 22, "le@wekciga.lr"),
("Lydia Thornton", "Puerto Rico", 19, "lefvoru@itbewuk.at"),
("Richard Newton", "Pitcairn Islands", 17, "da@lasowiwa.su"),
("Eric Townsend", "Svalbard & Jan Mayen", 22, "jijer@cipzo.gp"),
("Trevor Hines", "Dominican Republic", 15, "ev@hivew.tm"),
("Inez Little", "Namibia", 26, "meewi@mirha.ye"),
("Lloyd Aguilar", "Swaziland", 16, "oza@emneme.bb"),
("Erik Lane", "Turkey", 30, "efumazza@va.hn"),
]
adult = list(filter(lambda x:x[2] >= 20, people))
adult.sort(key==lambda x:x[2])
print(adult)'스파르타코딩클럽[AI트랙 3기] > 파이썬 과제' 카테고리의 다른 글
| [파이썬 원격 강의 과제]220915 #5 조건문, 반복문 (0) | 2022.09.21 |
|---|---|
| [파이썬 원격 강의 과제]220913 #3 클래스 활용해보기 (0) | 2022.09.21 |
| [파이썬 원격 강의 과제]220907 #2 숫자 야구 만들기 (1) | 2022.09.21 |
| [파이썬 원격 강의 과제] 220906 #1 계산기 만들어보기 (0) | 2022.09.18 |