Recap: Refactor a Pricing Tool
Lesson 26 of 48 in Coddy's Python for Lua 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 Lua Developers
1From Lua to Python
Run Your First Python ProgramPrinting and TextNames and Local ScopeNumbers and ConversionSuites Instead of endRecap: Rewrite a Receipt4Iteration and Transformations
Numeric for to rangepairs, ipairs, and itemsLoop Control and SkipsComprehensions and SortingRecap: Build a Ranking2Values and Decisions
nil Versus NoneTable Identity and EqualityTruthiness Is Differentelif and Value SelectionRecap: Eligibility Rules5Functions and Closures
Returns and Shared ParametersDefaults and Named CallsVarargs and Call UnpackingUpvalues and Late BindingRecap: Refactor a Pricing Tool3Tables to Typed Collections
Sequence Tables to ListsTuples and Multiple ReturnsRecords to DictionariesSets and Shared TablesRecap: Clean an Inventory6Errors and Modules
Input and Exact Textpcall to Exceptionsrequire to importHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler