https://sizzang97.tistory.com/87
Python ํน๊ฐ(5) (try-except, stacktrace, comprehension)
์ค๋๋ ํน๊ฐ๊ณผ ์ ํ ํฌ ํ์ด์ฌ์ ๊ฐ์ด ๊ณต๋ถํด๋ณด์! https://wikidocs.net/30 05-4 ์์ธ ์ฒ๋ฆฌ ํ๋ก๊ทธ๋จ์ ๋ง๋ค๋ค ๋ณด๋ฉด ์์์ด ๋ง์ ์ค๋ฅ๋ฅผ ๋ง๋๊ฒ ๋๋ค. ๋ฌผ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ์ด์ ๋ ํ๋ก๊ทธ๋จ์ด ์๋ชป ๋
sizzang97.tistory.com
๊ณ์ฐ๊ธฐ ๋ง๋ค๊ธฐ 2ํ!!
์ด์ class๋ฅผ ์ด์ฉํด์ ์ฌ์น์ฐ์ฐ์ ๊ตฌํํ๋ ๊ณ์ฐ๊ธฐ๋ฅผ ๋ง๋ค์๋๋ฐ
์ค๋ ๋ฐฐ์ด try/except๋ฅผ ์ด์ฉํด์ 0์ผ๋ก ๋๋ด์ ๋, ๋ฌธ์๋ฅผ ์ ๋ ฅํ์๋ ํ๋ฆฐํธํด์ฃผ์!!
class FourCal:
def __init__(self, first, second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def minus(self):
result = self.first - self.second
return result
def mul(self):
result = self.first * self.second
return result
def div(self):
result = self.first / self.second
return result
try:
cal = FourCal(4, 0)
print(cal.add())
print(cal.minus())
print(cal.mul())
print(cal.div())
except ZeroDivisionError:
print("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.")
except ValueError:
print("์ซ์๊ฐ ์๋๋๋ค.")
์ด๋ ๊ฒ ํ๊ณ ์ค๋ ํน๊ฐ๋ ํํฐ๋์ ๋ชจ๋ฒ๋ต์์ ํ์ธํ๋๋ฐ
ํํใ ํํํํํณ ๋์ฒ๋ผ ์ฐ๋ฉด ์ฌ์๋ ๊ฐ์ธ๋ฉด๋ด ๋ค์ด๊ฐ๊ฒ ๋ค~~~~~
์ ๋ ๊ฒ ํต์งธ๋ก ์ฐ๋๊ฑด ์์ข๋ค๊ณ ํ๋ค!
class FourCal():
def set_number(self, first, second):
self.first = first
self.second = second
def add(self):
result = self.first + self.second
return result
def minus(self):
result = self.first - self.second
return result
def mul(self):
result = self.first * self.second
return result
def div(self):
try:
return self.first / self.second
except ZeroDivisionError:
print("0์ผ๋ก ๋๋ ์ ์์ต๋๋ค.")
while True:
try:
first, second = map(int, input().split())
break
except ValueError:
print("์ซ์๋ฅผ ์
๋ ฅํ์ธ์")
cal = FourCal()
cal.set_number(first, second)
print(cal.add())
print(cal.minus())
print(cal.mul())
print(cal.div())
์ด๋ ๊ฒ ๋ฐ๊ฟ์ฃผ์ฅ...ใ ใ ....
์ ์ธ๋๋ ํด๋น ๋ถ๋ถ์ ๊ฐ์ try๋ฅผ ๊ฑธ์ด์ฃผ๋๊ฒ ์ข๋ค!
filter/sort ๊ด๋ จ ๊ณผ์
- filter ํน์ ๋ฆฌ์คํธ ์ถ์ฝ์์ ์ฌ์ฉํด ์ฝ๋๋ฅผ ์์ฑํด์ฃผ์ธ์
- ์ ๊ณต ๋ ์ฌ์ฉ์๋ค ์ค ๋์ด๊ฐ 20์ด ๋ฏธ๋ง์ธ ์ฌ๋๋ค์ ์ ์ธํด์ฃผ์ธ์
- ์ฌ์ฉ์๋ค์ ๋์ด ์์ผ๋ก ์ ๋ ฌํด์ฃผ์ธ์
์ด๊ฒ ๋ฌธ์ ์๋๋ฐ ๋ฌด๋ํ๊ฒ ํด๊ฒฐ~~!!~!
import pprint
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"),
]
people = list(filter(lambda x: x[2] >= 20, people))
people.sort(key=lambda x: x[2])
# some code
pprint.pprint(people)
"""
[('Winnie Hall', 'Palestinian Territories', 22, 'moci@pacivhe.net'),
('Brent Peterson', 'Svalbard & Jan Mayen', 22, 'le@wekciga.lr'),
('Eric Townsend', 'Svalbard & Jan Mayen', 22, 'jijer@cipzo.gp'),
('Inez Little', 'Namibia', 26, 'meewi@mirha.ye'),
('Carrie Palmer', 'Mauritius', 28, 'fenlofi@tor.aq'),
('Alfred Schwartz', 'Syria', 29, 'ic@tolseuc.pr'),
('Katharine Little', 'Anguilla', 29, 'am@kifez.et'),
('Peter Bowen', 'Burundi', 30, 'vinaf@rilkov.il'),
('Erik Lane', 'Turkey', 30, 'efumazza@va.hn')]
"""
'๐๐ช > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฑ๊ธํ, ์ ๋ ฅ๊ฐ, ๋ฐ๋ณต๋ฌธ ๊ณผ์ (0) | 2022.09.15 |
---|---|
Python ํน๊ฐ (6) (option, args/kwargs, packing/unpacking) (0) | 2022.09.15 |
Python ํน๊ฐ(5) (try-except, stacktrace, comprehension) (1) | 2022.09.14 |
๋ํ ๋์ด ๊ณ์ฐ, ๊ณ์ฐ๊ธฐ๋ง๋ค๊ธฐ, ํ๋กํ ๊ด๋ฆฌ๊ธฐ๋ฅ (0) | 2022.09.13 |
Python ํน๊ฐ (4) (class, mutable/immutable) (0) | 2022.09.13 |
๋๊ธ