Recap: Pricing Functions
Lesson 26 of 48 in Coddy's Python for C# Developers course.
Challenge
BeginnerRead nonnegative n, n lines of nonnegative unit-price/quantity pairs, and a nonnegative fixed discount. Sum line products into subtotal. Subtract the discount once, clamping at zero. Shipping is 0 at discounted amounts of at least 50, otherwise 5; an empty order instead costs 0 including shipping. Output subtotal discounted shipping total. Define and use functions for line pricing and order calculation; any implementation meeting the output contract is accepted.
C# transfer checkpoint
Port the value-returning helper, not a Console.WriteLine side effect. Python def does not need a declared result type.
C# reference excerpt: executable statements assume using System; type-only excerpts illustrate the declared contract.
static int Discount(int subtotal, int discount) => Math.Max(0, subtotal - discount);
Console.WriteLine(Discount(8, 10));Python pattern
def discount(subtotal, amount):
return max(0, subtotal - amount)
print(discount(8, 10))Editable implementation files: main.py. The locked driver.py is the supplied launcher or input/output harness and must remain unchanged.
Use the supplied input contract without prompts. Print only the required lines, each ending in a newline.
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 C# Developers
1C# Foundations in Python
Run the Python WorkspaceOutput, Strings, and CommentsTyped Locals to Python NamesDivision and RemaindersIndentation Instead of BracesRecap: Port a Receipt4Loops and Transformations
From foreach to forIndexes and Dictionary ItemsSentinels and Loop ControlFrom LINQ to ComprehensionsRecap: Stable Ranking2None and Decisions
Nullable Values and NoneEquality, Identity, and TypesTruth Tests and FallbacksBranch PriorityRecap: Eligibility Rules5Functions and Scope
Define Functions and ReturnNamed Arguments and DefaultsStar Arguments and UnpackingCallbacks and Python ScopeRecap: Pricing Functions3Collections and References
From List<T> to listTuples and UnpackingDictionary Keys and ValuesSets, Aliases, and CopiesRecap: Stock Lookup6Reliable Python Programs
Input and Exact OutputExceptions and ValidationImports and the Main GuardType Hints and Runtime TypesRecap: Validate a Batch9Project: Order Processor
Project 1: Order ModelProject 2: Pricing PoliciesPractice on your own: Online Python compiler