본문 바로가기

카테고리 없음

파이썬으로 음식 주문받는 프로그램 만들어보기

from posixpath import split
items = {
    'pizza': {"count":3, "options": ["갈릭 소스", "치즈 소스", "핫 소스"], 'price': 25000},
    'chicken': {"count":3, "options": ["반반", "순살", "소금"], 'price': 18000},
}
##################################################
#메뉴판이 이름: 가격 이렇게 나오게\

print("#"*50)
for name, detail in items.items():
    print("{}   {}".format(name, detail['price']))      # print(name, detail['price'])
print("#"*30)
customer_order = {}
while True:
    order = input('어떤 걸 주문하시겠어요?(q 누르면 주문종료.) "제품 수량" 형식으로 입력해주시렵니까 ?')
    if order == 'q' :break
    print(order.strip().split(" "))
    item_name, order_count = order.strip().split(" ")
    order_count = int(order_count)
    if items[item_name]['count'] < order_count:
        print("재고가 부족해요, {}개 까지 주문 가능합니다.".format(items[item_name]['count']))
        continue
    customer_order[item_name] = order_count
    print(customer_order)
#재고 업데이트 + 배송
for order_name, order_count in customer_order.items():
    items[order_name]['count'] -= order_count
#영수증
print("*"*20)
print("영수증")
total_price = 0
for order_name, order_count in customer_order.items():
    print("제품명 : {} 단가 : {} 주문수량 : {} 합계 : {}".format(order_name, items[order_name]['price'],order_count,items[order_name]['price']*order_count  ))
    total_price += items[order_name]['price'] * order_count
    print(f"총 이용금액 : {total_price}")
print("*"*20)
print("배송준비 -> 배송중 -> 배송완료")

 

중간중간 코드해설을 해보자.

10~11줄

for name, detail in items.items():
    print("{}   {}".format(name, detail['price']))

.items()는 파이썬 기본 내장 메서드다.

이거 출력하면 이렇게 나오는데, 키와 값에 해당하는 항목값을 추출해준다.

 

print(order.strip().split(" "))

이 친구를 출력하게 되면 리스트가 나온다. 사용자에게서 input값으로 (pizza 2)를 받았다면 ['pizza', '2']를 출력해준다.

strip으로 앞 뒤 공백 제거하고나서 띄어쓰기 한 칸 기준으로 값을 리스트에 넣어주는거다. 

item_name, order_count = order.strip().split(" ")

그리고나서 이렇게 코드를 써주면 앞서 말한 pizza와 개수가 item_name과 order_count에 들어간다. 

order라는 변수에 "." 두개 찍은 매서드가 바로 리스트로 반환이 된다니 간단하면서도 효율성이 좋다.

 

 

if items[item_name]['count'] < order_count:
	print("재고가 부족해요, {}개 까지 주문 가능합니다.".format(items[item_name]['count']))

 

이어서 현재 있는 내 물건의 재고 개수와 방금 input을 통해 주문받은 음식의 갯수를 비교한다. item_name은 방금 .split을 통해서 변수가 생겼으니 바로 사용 가능하다.

 

customer_order[item_name] = order_count

그리고 등장하는 이 코드를 통해서 비어있던 customer_order 딕셔너리가 채워진다.

 

for order_name, order_count in customer_order.items():
    items[order_name]['count'] -= order_count

 

 

채워진 딕셔너리를 바로 활용하는 코드. 

customer_order안에 있는 키-값 아이템들을 동시에 order_name과 order_count라는 변수로 넣어줌.

for loop 바로 아래에 있는 코드가 재고관리코드 / 주문받은만큼 재고에서 차감하라는 의미.