Menu
CoddyTech

프로젝트 1: 주문 모델

Coddy의 C# 개발자를 위한 Python 코스 레슨. 48개 중 43번째.

4단계 인메모리 주문 도구를 시작하세요. Order(customer, quantity, unit_cents) 및 subtotal()을 구현하세요. 입력은 세 줄입니다: 콜론이 없는 비어 있지 않은 고객 이름, 양수의 수량, 0 이상의 센트 단위 단가. customer:quantity:subtotal 형식으로만 출력하세요. 통화 금액은 정수로 유지하세요. 모델 계산을 완료하세요. 제공된 정책 및 헬퍼 스캐폴드는 이후 단계를 위해 예약되어 있습니다. 시작 코드는 실행 가능하지만 의도적으로 미완성된 소계를 계산합니다.

C# 전환 체크포인트

선언된 C# 필드를 Python 인스턴스 속성으로 변환하고 센트는 정수로 유지하세요. 이 발췌 부분은 전체 프로젝트가 아닌 모델 패턴을 보여줍니다.

C# 참고 발췌: 실행 가능한 문은 using System;을 가정하며, 타입 전용 발췌는 선언된 계약을 설명합니다.

class Line {
    public int Quantity;
    public int UnitCents;
    public int Subtotal() => Quantity * UnitCents;
}

Python 패턴

class Line:
    def __init__(self, quantity, unit_cents):
        self.quantity = quantity
        self.unit_cents = unit_cents
    def subtotal(self):
        return self.quantity * self.unit_cents

4단계 전체에 걸쳐 동일한 작업 공간 경로를 유지하세요. 일반적인 import가 모듈을 연결하며, 드라이버가 main()을 시작하고 별도의 API 검사를 수행합니다. 이전 출력을 대체할 때 현재 단계의 출력 계약을 따르세요.

challenge icon

챌린지

초급

4단계 인메모리 주문 도구를 시작합니다. Order(customer, quantity, unit_cents)와 subtotal()을 구현하세요. 입력은 세 줄입니다: 콜론이 없는 비어 있지 않은 고객 이름, 양수의 수량, 0 이상의 센트 단위 단가. customer:quantity:subtotal만 출력하세요. 금액은 정수로 유지하세요. 모델 계산을 완료하세요. 제공된 정책 및 헬퍼 스캐폴드는 이후 단계를 위해 남겨두었습니다. 시작 코드는 실행 가능하지만 의도적으로 완성되지 않은 소계를 계산합니다.

편집 가능한 구현 파일: order.py, standard_policy.py, bulk_policy.py, functions.py, main.py. 잠긴 driver.py는 제공된 실행기 또는 입력/출력 하네스이며 변경되지 않은 상태로 유지되어야 합니다.

프롬프트 없이 제공된 입력 계약을 사용하세요. 각 줄이 줄바꿈으로 끝나는 필요한 줄만 출력하세요.

4개 단계 모두 order.py, standard_policy.py, bulk_policy.py, functions.py, main.py, driver.py를 유지합니다. Order는 order.py에, 정책 클래스는 해당 이름의 파일에, make_policy와 parse_order는 functions.py에 있어야 하며, main.py는 main() 내부에서 입출력을 담당합니다. 이 단계는 제공된 스캐폴드에서 시작합니다. 현재 잠긴 드라이버는 main을 한 번 호출한 후 추가 입력이나 출력 없이 API를 확인합니다.

직접 해보기

from order import Order
from standard_policy import StandardPolicy
from bulk_policy import BulkPolicy
from functions import make_policy, parse_order
from main import main

main()

probe_order = Order("probe", 3, 7)
assert (probe_order.customer, probe_order.quantity, probe_order.unit_cents) == ("probe", 3, 7)
assert probe_order.subtotal() == 21
probe_order.quantity = 4
assert probe_order.subtotal() == 28
other_order = Order("other", 1, 0)
assert other_order.subtotal() == 0 and probe_order.subtotal() == 28

C# 개발자를 위한 Python의 모든 레슨

직접 연습해 보세요: 온라인 Python 컴파일러