업무에 파이썬 활용할 줄 알기
Day15 | 중급 | 커피 머신 프로젝트 본문
게임 쪼개어보기
재료 넉넉, 돈도 넉넉 (거스름돈 계산)
재료 넉넉, 돈은 모자라게
재료 모자랄 때
재료가 충분한지 안한지 판단하는 단계
시행착오
내가 어떤 코드를 짜려다가 궁지에 몰렸지?
- 남아있는 ingredient의 양을 계산해주는 def문을 작성하고자 함. → 필요없었던 단계였음
ㄴ 그러다보니 남아있는 ingredient의 양을 resource에 반영하는 코드를 짜려고 함 - 리소스가 충분한지 안한지 판단하고자 함
모범답안
- 시행착오의 원인
- def문의 역할을 정하지 못함
- def문에 변수를 적절하게 사용해주지 못함
- for문을 어떻게 만들어줘야할지 몰랐음.
즉, 네스팅 딕셔너리 형태에서 범위에 해당하는걸 어떻게 작성해줘야할지 몰랐음
- 깨달은 점
생각의 과정이 아래와 같아야 한다
- 어떤 코드가 필요한지 정의한다
e.g.
충분한지, 충분하지 않은지를 판단한다
충분하지 않을 때 문구를 반환한다
충분할 때 돈을 입력하는 다음 단계로 넘어갈 수 있도록 한다 - 판단의 기준(계산법)을 구현한다
e.g.
Water, milk, coffee 세가지 재료각각에 대해 resource와 비교한다 > for문 - ‘무엇에 대해 판단할지’가 변수이므로 def문의 변수로 써준다
e.g.
내가 그냥 작성한 코드가 어떻게 def문으로 쓰이는지 예시
최종 모범답안
MENU = {
"espresso": {
"ingredients": {
"water": 50,
"coffee": 18,
},
"cost": 1.5,
},
"latte": {
"ingredients": {
"water": 200,
"milk": 150,
"coffee": 24,
},
"cost": 2.5,
},
"cappuccino": {
"ingredients": {
"water": 250,
"milk": 100,
"coffee": 24,
},
"cost": 3.0,
}
}
resources = {
"water": 300,
"milk": 200,
"coffee": 100,
}
def is_resource_sufficient(order_ingredients):
for item in order_ingredients:
if order_ingredients[item] >= resources[item]:
print(f"Sorry there is not enough {item}.")
return False
return True
def process_coins():
print("Please insert coins.")
total = int(input("how many quarters?: ")) * 0.25
total += int(input("how many dimes?: ")) * 0.1
total += int(input("how many nickels?: ")) * 0.05
total += int(input("how many pennies?: ")) * 0.01
return total
def is_payment_sufficient(money_received, drink_cost):
global profit
if money_received >= drink_cost:
profit += money_received
left_money = round(money_received - drink_cost, 2)
print(f"Here is ${left_money} dollars in change.")
return True
else:
print("Sorry that's not enough money, Money refunded.")
return False
def make_coffee(drink_name, order_ingredient):
for item in order_ingredient:
resources[item] -= order_ingredient[item]
print(f"Here is your {drink_name}. Enjoy!")
is_on = True
profit = 0
while is_on:
choice = input("What would you like? (espresso/latte/cappuccino): ")
if choice == "off":
is_on = False
elif choice == "report":
print(f"Water: {resources['water']}ml")
print(f"Milk: {resources['milk']}ml")
print(f"Coffee: {resources['coffee']}g")
print(f"Money: ${profit}")
else:
drink = MENU[choice]
if is_resource_sufficient(drink["ingredients"]):
#TODO 돈 넣으라고 메세지창 띄우고, 토탈값을 계산한다
payment = process_coins()
#TODO 충분한 돈을 넣었는지 확인한다
## 충분치 않으면 환불한다는 메세지창을 띄운다
## 충분하면 profit에 추가 후, 거스름돈 메세지창을 띄운다(2 dicimal places)
if is_payment_sufficient(payment, drink["cost"]):
# TODO 커피를 만든다
##리소스에서 사용한 재료양만큼을 뺀다
##Here is your latte. Enjoy! 메세지창을 띄운다
make_coffee(choice, drink["ingredients"])
'Python > [Udemy] 100개의 프로젝트로 Python 개발 완전 정복' 카테고리의 다른 글
Day 18 | 중급 | 터틀 & 그래픽 사용자 인터페이스 (GUI) (2) | 2024.01.02 |
---|---|
Day17 | 중급 | 퀴즈 프로젝트와 OOP의 장점 (0) | 2023.12.28 |
Day16 | 중급 | 객체 지향 프로그래밍(OOP) (0) | 2023.12.27 |
Day12 | 초급 | 유효 범위와 숫자 맞추기 게임 복습 (0) | 2023.12.20 |
Day11 | 초급 | 블랙잭 게임 프로젝트 (0) | 2023.12.18 |