Create the Order Model
Lesson 43 of 48 in Coddy's Python for GDScript Developers course.
Build one order processor through four connected steps: model an order, delegate pricing, validate records, and aggregate a report. Transfer GDScript collaborator-based design to ordinary Python modules without Nodes, signals, or frame callbacks. All six files remain present. Preserve previous behavior unless a task explicitly changes it. The program uses integer cents and in-memory objects.
Challenge
BeginnerBuild an in-memory order processor. This first step models one order; later steps add pricing, reject bad records, and aggregate a report. Implement Order(customer, quantity, unit_cents) and subtotal(), then use them in a complete program. Input is three lines: a nonempty customer name with no colon, a positive integer quantity, and a nonnegative integer unit price in cents. Print exactly customer:quantity:subtotal and a newline. Keep money as integer cents. No prompts, currency symbols, or decimal conversion. The starter runs but its subtotal is intentionally unfinished.
Keep the named classes, methods, and functions in the complete program. After normal execution, a locked driver calls these APIs on fresh objects and asserts their behavior without printing or reading more input.
For step 1, complete Order.subtotal() in order.py. The policy classes and helper functions are unfinished scaffolds for later steps; leave them in place. The imports and entry-point wrapper are supplied scaffolding, so no advanced module setup is expected. For example, from order import Order makes the class in order.py available in another file. Importing these modules does not read input; execution starts in the locked driver.
All four steps use the same files. Edit order.py for Order, standard_policy.py and bulk_policy.py for the pricing classes, functions.py for make_policy and parse_order, and main.py for input and output inside main(). driver.py is locked: it calls main() once, then runs the supplied API checks.
Try it yourself
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
All lessons in Python for GDScript Developers
1From GDScript to Python
Run Your First Python ProgramPrint Is Not printsVar, Const, and BindingsInteger Division DifferencesSimilar Indentation, New ScopeRecap: Rewrite a Receipt4Iteration and Transformations
Ranges and RebindingItems and enumerateWhile and Early ExitArray Callbacks and KeysRecap: Build a Ranking2Values and Decisions
Null and NoneThe is Keyword Changes MeaningLogical Results and OperandsConditional ExpressionsRecap: Eligibility Rules3Collections and Shared State
Array and List APIsArrays, Vectors, and TuplesDictionary Lookup RulesDuplicate and Shallow CopyRecap: Clean an Inventory6Input, Errors, and Modules
Console Work Outside GodotValidation and ExceptionsScript Resources and ImportsTyped GDScript and AnnotationsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler