정말 어려웠던 프로젝트였다. 비록 코드가 효율적이지 않고 가독성이 떨어질 순있지만 최대한 노력해보았다.
다음에 시간이나면 GUI 와 함께 수정해보고싶다.
Coffee Machine Program Requirements
1. Prompt user by asking “What would you like? (espresso/latte/cappuccino):”
a. Check the user’s input to decide what to do next.
b. The prompt should show every time action has completed, e.g. once the drink is
dispensed. The prompt should show again to serve the next customer.
2. Turn off the Coffee Machine by entering “off” to the prompt.
a. For maintainers of the coffee machine, they can use “off” as the secret word to turn off
the machine. Your code should end execution when this happens.
3. Print report.
a. When the user enters “report” to the prompt, a report should be generated that shows
the current resource values. e.g.
Water: 100ml
Milk: 50ml
Coffee: 76g
Money: $2.5
4. Check resources sufficient?
a. When the user chooses a drink, the program should check if there are enough
resources to make that drink.
b. E.g. if Latte requires 200ml water but there is only 100ml left in the machine. It should
not continue to make the drink but print: “Sorry there is not enough water.”
c. The same should happen if another resource is depleted, e.g. milk or coffee.
5. Process coins.
a. If there are sufficient resources to make the drink selected, then the program should
prompt the user to insert coins.
b. Remember that quarters = $0.25, dimes = $0.10, nickles = $0.05, pennies = $0.01
c. Calculate the monetary value of the coins inserted. E.g. 1 quarter, 2 dimes, 1 nickel, 2
pennies = 0.25 + 0.1 x 2 + 0.05 + 0.01 x 2 = $0.52
6. Check transaction successful?
a. Check that the user has inserted enough money to purchase the drink they selected.
E.g Latte cost $2.50, but they only inserted $0.52 then after counting the coins the
program should say “Sorry that's not enough money. Money refunded.”.
b. But if the user has inserted enough money, then the cost of the drink gets added to the
machine as the profit and this will be reflected the next time “report” is triggered. E.g.
Water: 100ml
Milk: 50ml
Coffee: 76g
Money: $2.5
c. If the user has inserted too much money, the machine should offer change.
위 룰을 기반으로 작성한 코드
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 check_ingredients(menu):
ingredient = MENU[menu]["ingredients"]
return ingredient
def sufficient(menu) :
for n in resources :
for i in check_ingredients(menu) :
if resources[n] < check_ingredients(menu)[i] :
return False
else :
return True
def minus(menu) :
if menu == "espresso" :
resources["water"] -= check_ingredients(menu)["water"]
resources["coffee"] -= check_ingredients(menu)["coffee"]
else :
resources["water"] -= check_ingredients(menu)["water"]
resources["milk"] -= check_ingredients(menu)["milk"]
resources["coffee"] -= check_ingredients(menu)["coffee"]
def process_coin(quarters, dimes, nickles, pennies, menu) :
money = quarters * 0.25 + dimes * 0.1 + nickles * 0.05 + pennies * 0.01
if money > MENU[menu]["cost"] :
print(f"잔돈은 : {money - MENU[menu]['cost']} 입니다.")
if "money" not in resources :
resources["money"] = MENU[menu]["cost"]
else :
resources["money"] += MENU[menu]["cost"]
print(f"주문하신 {menu}입니다.")
elif money < MENU[menu]["cost"] :
print(f"돈이 모자랍니다. 잔돈 {money}를 돌려드릴게요")
def game() :
run_machine = True
while run_machine :
user_choice = input("어떤 음료를 드시겠습니까? (espresso/latte/cappuccino) > ")
if user_choice == "report" :
print(resources)
elif user_choice == "off" :
run_machine = False
print("작동을 종료합니다.")
elif not sufficient(user_choice) :
print("재료가 부족합니다.")
elif sufficient(user_choice) :
print("동전을 넣어주세요")
quarters = int(input("quarter는 몇개인가요?"))
dimes = int(input("dimes는 몇개인가요?"))
nickles = int(input("nickles는 몇개인가요?"))
pennies = int(input("pennies는 몇개인가요?"))
process_coin(quarters,dimes,nickles,pennies,user_choice)
minus(user_choice)
game()