반응형
이번 포스팅은 파이썬으로 김밥헤븐을 만드는 코드를 소개합니다.
restaurant.py 파일 만들기
VS Code를 실행하여 restaurant.py 파일을 만들어 준비합니다.
# -*- coding: utf-8 -*-
class Comments:
title = "#### %s 레스토랑에 오신걸 환영합니다. ####"
product_description = "%s:%s(%s원)"
insert_price = "\n요금을 넣어 주세요. : "
insufficient_price = "%s 요금이 부족합니다. 거스름돈은 %s원 입니다."
select_menu = "원하시는 메뉴를 선택하세요."
select_error = "잘못 입력하셨습니다."
finish_sale = "선택하신 %s 입니다. 거스름돈은 %s원 입니다.\n감사합니다."
terminate_sale = "주문을 종료합니다."
class Menus:
food_names = []
food_prices = []
레스토랑 파일 내에 두 개의 클래스를 선언합니다.
order.py 파일 만들기
order.py 파일을 만들어 주문받을 함수들을 입력해줍니다.
# -*- coding: utf-8 -*-
from restaurant import Comments, Menus
all_menus = [
{"menu" : "김밥", "price" : 3000},
{"menu" : "우동", "price" : 5000},
{"menu" : "라면", "price" : 6000},
{"menu" : "순대", "price" : 4000},
]
class Order(Menus):
#클래스 변수 정의
data = all_menus
name = "김밥헤븐"
status = True
def __init__(self):
print(Comments.title % self.name)
# 메뉴 지정
def set_products(self):
Menus.food_names = []
Menus.food_prices = []
for food in self.data:
Menus.food_names.append(food["menu"])
Menus.food_prices.append(food["price"])
def run(self):
# 메뉴 등록 후, 반영 (시스템)
self.set_products()
while self.status:
try:
inputMoney = int(input(Comments.insert_price))
except ValueError:
print(Comments.select_error)
else:
self.select_menu(inputMoney)
def select_menu(self, money):
print(Comments.select_menu)
for idx, name in enumerate(Menus.food_names):
price = Menus.food_prices[idx]
print(Comments.product_description %(str(idx), name, price))
inputFood = int(input(Comments.select_menu))
self.payment(money, inputFood)
def payment(self, money, idx):
name = Menus.food_names[idx]
price = Menus.food_prices[idx]
if money >= price:
balance = money - price
print(Comments.finish_sale %(name,str(balance)))
else:
print(Comments.insufficient_price %(name,str(money)))
if __name__=="__main__":
order = Order()
try:
order.run()
except KeyboardInterrupt:
order.status = False
print(Comments.terminate_sale)
위와 같이 입력하여 실행하면 키오스크 같은 프로그램을 만들 수 있습니다.
# 실행코드
python order.py
# 반복문 나가는 코드
ctrl+c
[출처] 휴먼교육센터 - Evan 강사님
반응형
'Python' 카테고리의 다른 글
[Python] 구글 코랩 .ipynb파일을 HTML파일로 변환하기 (0) | 2023.01.12 |
---|---|
[Python] google colab 에서 구글 드라이브 연동하는 방법 (0) | 2022.12.28 |
[Python] 기초 문법 정리(숫자형, 문자열, 리스트, 튜플, 딕셔너리) (0) | 2022.12.26 |
[Python] VS Code 에서 파이썬 가상환경 설정 방법 (0) | 2022.12.23 |
[Python] 파이썬을 설치하는 두 가지 방법 (0) | 2022.12.23 |