Recap: Refactor a Pricing Tool
Lesson 26 of 48 in Coddy's Python for Dart 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 Dart Developers
1From Dart to Python
Run Your First Python ProgramInterpolation and printvar, final, and Python NamesFloor Versus TruncationBraces and Block ScopeRecap: Rewrite a Receipt4Iteration and Transformations
for-in and rangeentries and enumerateLoop Updates and continuewhere, map, and List BuildingRecap: Build a Ranking2Values and Decisions
Null Safety and NoneList Equality ChangesConditions and Null DefaultsTernaries and elifRecap: Eligibility Rules5Functions and Closures
Arrows and Explicit ReturnsNamed Calls and DefaultsExplicit Variadic ArgumentsClosures and nonlocalRecap: Refactor a Pricing Tool3Collections and References
Lists and Negative IndexesRecords and Tuple UnpackingMap Lookups and KeyErrorSets and Shared ObjectsRecap: Clean an Inventory6Reliable Python Programs
stdin and Exact Formattingon, catch, finally, and withLibraries and Import ExecutionStatic Types Versus HintsRecap: Validate a Batch9Project: Order Processing
Create the Order ModelAdd Pricing PoliciesPractice on your own: Online Python compiler