1번

file = open("2024100-01.txt", "w")
coke_count = int(input("Coke count: "))
print(f"Coke count is {coke_count}")
file.write(f"Coke count is {coke_count}\\n")
while coke_count > 0:
money = int(input("Insert money: "))
print(f"{money} won received.")
file.write(f"{money} won received.\\n")
if money >= 1000:
change = money - 1000
print(f"Change is {change} won.")
file.write(f"Change is {change} won.\\n")
coke_count -= 1
else:
print("Not enough money. Please try again.")
file.write("Not enough money. Please try again.\\n")
print("Sold out.")
file.write("Sold out.\\n")
file.close()
1번 심화
문제: 자판기 프로그램
- 패키지를 활용하여 자판기 프로그램을 설계하세요.
- 패키지 이름:
vending_machine
- 모듈:
money_manager
: 돈을 처리하는 기능.
inventory_manager
: 재고를 관리하는 기능.
exceptions
: 사용자 정의 예외 처리 기능.
__init__.py
: 패키지 초기화를 포함.
- 프로그램 흐름:
- 사용자가 판매할 콜라의 개수를 입력합니다.
- 돈을 입력받아 콜라를 판매하며, 잔돈을 계산하여 출력합니다.
- 돈이 부족하거나, 잘못된 입력이 주어질 경우 예외를 처리합니다.
- 콜라가 모두 소진되면 프로그램이 종료됩니다.
- 모든 출력은
output.txt
파일에 기록됩니다.
파일 구조
vending_machine/
__init__.py
money_manager.py
inventory_manager.py
exceptions.py
main.py
output.txt
패키지 코드
1. vending_machine/__init__.py
from .money_manager import MoneyManager
from .inventory_manager import InventoryManager
from .exceptions import InsufficientFundsError, SoldOutError, InvalidInputError
2. vending_machine/money_manager.py
class MoneyManager:
def __init__(self, price_per_item):
self.price = price_per_item
def process_payment(self, money):
if money < self.price:
raise InsufficientFundsError(f"돈이 부족합니다. 최소 {self.price}원이 필요합니다.")
return money - self.price