Coding/Python

[python] ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ€ด์ฆˆ

๊น€์œ ๋‹ˆ์ฝ˜ 2021. 11. 4. 18:35

#์ž๋™ ์ฃผ๋ฌธ ์‹œ์Šคํ…œ: ์‹œ์Šคํ…œ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๊ณ  ์ ์ ˆํ•œ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์„ ๋„ฃ์œผ์‹œ์˜ค

# ์กฐ๊ฑด1: 1๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ์ž…๋ ฅ๊ฐ’์ด ๋“ค์–ด์˜ฌ๋•Œ๋Š” ValueError๋กœ ์ฒ˜๋ฆฌ
# ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€ : "์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. "

# ์กฐ๊ฑด2: ๋Œ€๊ธฐ ์†๋‹˜์ด ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋Š” ์น˜ํ‚จ๋Ÿ‰์€ 10๋งˆ๋ฆฌ๋กœ ํ•œ์ •
# ์น˜ํ‚จ ์†Œ์ง„์‹œ ์‚ฌ์šฉ์ž ์ •์˜ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ณ  ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ
# ์ถœ๋ ฅ ๋ฉ”์‹œ์ง€: "์žฌ๊ณ ๊ฐ€ ์†Œ์ง„๋˜์–ด ๋” ์ด์ƒ ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค."

 

 

 

 

class TooManyChickenError(Exception):
pass


chicken = 10
waiting = 1 #ํ™€ ์•ˆ์—๋Š” ๋งŒ์„ ๋Œ€๊ธฐ๋ฒˆํ˜ธ 1๋ฒˆ๋ถ€ํ„ฐ ์‹œ์ž‘

while(True):
try:
print("๋‚จ์€ ์น˜ํ‚จ : {0}".format(chicken))
order = int(input("์น˜ํ‚จ ๋ช‡ ๋งˆ๋ฆฌ ์ฃผ๋ฌธํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?"))
if order > chicken:
print("์žฌ๋ฃŒ๊ฐ€ ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค.")
elif order <= 0:
print("์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์˜€์Šต๋‹ˆ๋‹ค.")
else:
print("[๋Œ€๊ธฐ๋ฒˆํ˜ธ {0}] {1} ๋งˆ๋ฆฌ ์ฃผ๋ฌธ์ด ์™„๋ฃŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."\
.format(waiting, chicken))
waiting += 1
chicken -= order

if chicken == 0:
raise TooManyChickenError
except ValueError:
print("์ž˜๋ชป๋œ ๊ฐ’์„ ์ž…๋ ฅํ•˜์…จ์Šต๋‹ˆ๋‹ค. ")

except TooManyChickenError:
print("์žฌ๊ณ ๊ฐ€ ์†Œ์ง„๋˜์–ด ๋” ์ด์ƒ ์ฃผ๋ฌธ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")
break