Menu
CoddyTech

Recap: Pricing Functions

Lesson 26 of 48 in Coddy's Python for C# Developers course.

challenge icon

Challenge

Beginner

Read 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

Practice on your own: Online Python compiler