Recap: Refactor a Pricing Tool
Lesson 26 of 48 in Coddy's Python for Swift 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 Swift Developers
1From Swift to Python
Run Your First Python ProgramPrinting and Stringslet, var, and RebindingInt Range and DivisionIndentation and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for and Half-Open Rangesenumerated to enumeratewhile and Loop Controlmap, filter, ComprehensionsRecap: Build a Ranking2Values and Decisions
Optionals and NoneEquatable and IdentityBool Conditions and FallbacksBranches and TernariesRecap: Eligibility Rules5Functions and Closures
Functions and Explicit ReturnArgument Labels and DefaultsVariadics and Call ExpansionCapture Lists and DefaultsRecap: Refactor a Pricing Tool3Value Types to Shared Objects
Array to listTuples and UnpackingDictionary Lookup and OrderValue Copies and AliasesRecap: Clean an Inventory6Errors and Modules
Input and String Formattingthrows, defer, and withModules and Main GuardsHints and Runtime TypesRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler