Day 015

AWESOMee·2022년 2월 4일
0

Udemy Python Bootcamp

목록 보기
15/64
post-thumbnail

Udemy Python Bootcamp Day 015

IDE
Intergrated Development Environment

Coffe Machine

# What i wrote
from menu import MENU, resources

# TODO: 3. Print report
drink_cost = 0
drink_water = 0
drink_milk = 0
drink_coffee = 0

remain_water = resources["water"]
remain_milk = resources["milk"]
remain_coffee = resources["coffee"]
def report(remain_water, remain_milk, remain_coffee):
    remain_water -= drink_water
    remain_milk -= drink_milk
    remain_coffee -= drink_coffee

def insert_coins():
    print("Please insert coins.")
    quarter = int(input("how many quarters?: "))
    dime = int(input("how many dimes?: "))
    nickle = int(input("how many nickles?: "))
    penny = int(input("how many pennies?: "))
    inserted = (quarter * 0.25) + (dime * 0.1) + (nickle * 0.05) + (penny * 0.01)
    if inserted < drink_cost:
        print("Sorry that's not enough money. Money refunded.")
    else:
        change = round(inserted - drink_cost, 2)
        print(f"Here is ${change} in change.")

money = 0
# TODO: 1. Prompt user by asking "What would you like? (espresso/latte/cappuccino):"
def game(remain_water, remain_milk, remain_coffee, money):

    turn_off = True
    choose = input(" What would you like? (espresso/latte/cappuccino): ").lower()
    # TODO: 2. Turn off the Coffee Machine by entering "off" to the prompt
    while turn_off:
        if choose == "off":
            return False
        elif choose == "report":
            report(remain_water, remain_milk, remain_coffee)
            print(f"Water: {remain_water}ml\nMilk: {remain_milk}ml\nCoffee: {remain_coffee}g\nMoney: ${money}")
            game(remain_water, remain_milk, remain_coffee, money)
        # TODO: 4. Check resources sufficient?
        else:
            drink_water = MENU[f"{choose}"]["ingredients"]["water"]
            drink_cost = MENU[f"{choose}"]["cost"]
            out_of_stock = True
            while out_of_stock:
                if drink_water > resources["water"]:
                    print("Sorry there is not enough water.")
                    out_of_stock = False
                if choose != "espresso":
                    drink_milk = MENU[f"{choose}"]["ingredients"]["milk"]
                    if drink_milk > resources["milk"]:
                        print("Sorry there is not enough milk.")
                        out_of_stock = False
                drink_coffee = MENU[f"{choose}"]["ingredients"]["coffee"]
                if drink_coffee > resources["coffee"]:
                    print("Sorry there is not enough coffee.")
                    out_of_stock = False
                # TODO: 5. Process coins.
                insert_coins()
                print(f"Here is your {choose}. Enjoy!")
                
                money += drink_cost
                game(remain_water, remain_milk, remain_coffee, money)


game(remain_water, remain_milk, remain_coffee, money)
# TODO: 6. Check transaction successful?
# TODO: 7. Make Coffee.

0에서 이만큼 일궈낸거 잘했는데....
espresso/latte/cappuccino 선택후에
report 했을 때 remain적용이 절대절대 안돼서.... solution..

# Solution
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,
    }
}

profit = 0
resources = {
    "water": 300,
    "milk": 200,
    "coffee": 100,
}


def is_resource_sufficient(order_ingredients):
    """Returns True when order can be made, False if ingredients are insufficient."""
    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():
    """Returns the total calculated from coins inserted."""
    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 nickles?: ")) * 0.05
    total += int(input("how many pennies?: ")) * 0.01
    return total


def is_transaction_successful(money_received, drink_cost):
    """Return True when the payment is accepted, or False if money is insufficient."""
    if money_received >= drink_cost:
        change = round(money_received - drink_cost, 2)
        print(f"Here is ${change} in change.")
        global profit
        profit += drink_cost
        return True
    else:
        print("Sorry that's not enough money. Money refunded.")
        return False


def make_coffee(drink_name, order_ingredients):
    """Deduct the required ingredients from the resources."""
    for item in order_ingredients:
        resources[item] -= order_ingredients[item]
    print(f"Here is your {drink_name} ☕️. Enjoy!")


is_on = True

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"]):
            payment = process_coins()
            if is_transaction_successful(payment, drink["cost"]):
                make_coffee(choice, drink["ingredients"])

solution볼 때마다 드는 생각인데
진짜 이렇게 def 깔끔하고 이렇게 간단하게 코드 짠다고..? 라는 생각밖에 .....

for loop & list 사용하면 간단해질 수 있다는 거 아는데,,
아직 손에 안익었나보다 ㅠ 활용을 못하겠음..

profile
개발을 배우는 듯 하면서도

0개의 댓글