Recap: Refactor a Pricing Tool
Lesson 26 of 48 in Coddy's Python for GDScript Developers course.
Challenge
BeginnerPreserve this pricing behavior while separating the calculations into functions: each record contains nonnegative integer unit price and quantity; its subtotal is their product. An order subtotal is the sum of all line subtotals. Apply a fixed discount once to the order, but never reduce the result below zero. Shipping is 0 when the discounted amount is at least 50, otherwise 5. An empty order costs 0 including shipping. Read nonnegative record count n, then n lines of price quantity, then a nonnegative discount line. Print subtotal discounted shipping total on one line. Define and use functions for line pricing and order calculation; grading accepts any implementation preserving this output contract.
Your code goes in main.py. The locked driver.py starts your program and cannot be edited.
Try it yourself
# Starts the program in main.py. Edit main.py to solve the challenge.
from runpy import run_module
run_module("main", run_name="__main__")
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 Rules5Functions and Closures
Func, Def, and Returned ValuesKeyword Calls and DefaultsCallv and Python UnpackingCaptured Values and CallablesRecap: Refactor a Pricing Tool3Collections 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