파이썬 - 15일차 - 중급 - 로컬 개발 환경 설정과 커피머신

Shy·2023년 2월 20일

Python

목록 보기
3/9

파이썬 설치법

IDE는 통합 개발 환경이다. 우리의 코드를 작성 효율을 높여준다

  • 올바른 문체로 코드를 작성하게 도와주고, 디버그를 해준다.

우리가 사용할 IDE는 파이썬에 특화된 파이참이다.
파이참을 설치하기 전에 최신버전의 파이썬을 설치해야 한다.

파이썬_설치

저거 눌러서 확인 눌러주고 Close로 닫으면 된다.

파이참 다운로드

파이참_다운로드
Community 버전을 다운로드 하면 된다.

새로운 프로젝트를 만들 때, 최신버전의 파이썬과 연결해야 한다.

적당한 코드를 입력하여 잘 실행되는지 확인한다.

파이참의 매력 포인트

  • SpellCheck(맞춤법 검사): 코드, 변수 등을 입력 할 때, 잘 입력했는지 체크해준다.
  • 개발공간이 넓다. - 여러개의 창을 띄워놓고(분할 화면) 보면서 코딩이 가능하다.
  • Built-In Linter: 지정된 코딩 스타일에 맞지 않는 코드를 골라낸다. 프로그램의 일관적인 규칙(코드 스타일)을 지키게 도와준다. (지금은 PEP8을 따른다.)
  • LocalHistory : 코딩한 기록을 볼 수 있다. (지난 12시간 동안)
  • Structure : 모든 함수와 변수가 분류되어 나타난다. 그 함수의 위치를 알려주고 편집을 할 수 있게 해준다.
  • 함수를 고칠 때, Refactor-Rename을 사용하면, 밑에 사용한 함수들의 기존 이름들이 고쳐진다. 모든 위치에서 이름이 바뀐다. 찾고 바꾸기 보다 안전하다. (print구문 안의 함수들은 그냥 텍스트로 인식한다.)

커피 머신 프로젝트 소개 및 프로그래밍 요구 사항

  • 커피자판기를 코딩하는 것이다.
  • 실제 커피 자판기의 특징과 기능을 구현한다.

에스프레소, 라떼, 카푸치노를 만든다.

  • 에스프레소는 50ml물, 18g 커피가 들어간다. 가격:1.5달러
  • 라떼는 200ml물, 24g커피, 150ml우유가 들어간다. 가격:2.5달러
  • 카푸치노는 250ml물, 24g커피, 100ml우유가 들어간다. 가격:3달러
  • 커피머신 시작에는, 300ml물, 100g커피, 200ml우유를 채워야 한다.
  • 동전을 넣어서 작동시킨다.
  • 들어갈 수 있는 동전은 Penny(1cent), Nickel(5cents), Dime(10cents), Quarter(25cents)가 있다.

프로그램 요구 사항

  • report를 입력하면, 재료가 얼마나 남았고 돈이 얼마나 있는지 확인 가능하다.
  • 재료가 충분한지, 잔돈을 거슬러 줄 동전이 충분한지 확인이 가능해야 한다. (Check resources sufficient)
  • 반드시 코인을 넣어야 한다.
  • 커피 가격을 기준으로, 동전을 거슬러 줘야 한다.
  • 동전을 충분히 넣지 않았다면, 동전을 반환해주고 커피를 만들어 주지 않는다.
  • 동전을 충분히 넣었다면, 동전을 거슬러주며 커피를 만들어준다.

코드


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"])
                
profile
신입사원...

0개의 댓글