Complete Python Series · Part 25 of 29
Assignment 12-discount-engine
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Dynamic Course Discount Engine
Learn to create reusable discount policies using nested functions, closures, lexical scope, type hints and higher-order functions.
Problem statement
NextGen CTO offers promotional discounts on course purchases. Each policy has a minimum cart amount (threshold) and a discount rate. Create a function factory that returns an independently configured discount function.
Task requirements
- Define
create_discount_policy(threshold: float, rate: float). - Define an inner function accepting
amount: floatand returning a float. - Apply the discount when the amount is greater than or equal to the threshold; otherwise return the original amount.
- Return the inner function, without calling it.
- Create festival and student discount policies using the specified thresholds and rates.
- Test the policies with cart amounts of 6000.0 and 2500.0, respectively, and format the results to two decimal places.
Constraints
- Use nested functions and closures; do not use classes or global variables for policy configuration.
- Represent rates as fractions (e.g., 0.20 for 20%).
- Assume non-negative amounts and thresholds and rates between 0 and 1.
- Use the illustrative pricing rules provided above.
Good luck trying the exercise!
Starter code
def create_discount_policy(threshold: float, rate: float):
# TODO: Define an inner function that accepts amount
# TODO: Apply the discount if the amount meets the threshold
# TODO: Return the original amount otherwise
# TODO: Return the inner function
pass
# TODO: Create the festival_sale and student_discount policies
cart_1 = 6000.0
cart_2 = 2500.0
# TODO: Call each policy and print the formatted results
Hints
- An inner function can access variables defined in its enclosing function.
- Use
>=to check whether the cart qualifies for a discount. - Calculate the discounted amount using
amount - (amount * rate). - Return the inner function by its name, without parentheses.
- Each call to the outer function creates a new closure with its own captured values.
Solution: NextGen CTO Dynamic Course Discount Engine
The solution uses a function factory to create two independently configured discount functions. Each closure retains access to its own threshold and rate after the outer function returns.
def create_discount_policy(threshold: float, rate: float):
# Define the inner function
def apply_discount(amount: float) -> float:
if amount >= threshold:
return amount - (amount * rate)
return amount
# Return the inner function
return apply_discount
# Create reusable discount policies
festival_sale = create_discount_policy(5000, 0.20)
student_discount = create_discount_policy(2000, 0.10)
# Test cases
cart_1 = 6000.0
cart_2 = 2500.0
print(f"Festival Sale: INR {festival_sale(cart_1):.2f}")
print(f"Student Discount: INR {student_discount(cart_2):.2f}")
Expected output
Festival Sale: INR 4800.00
Student Discount: INR 2250.00
Solution explanation
- Line 1: Define the outer function with typed threshold and rate parameters.
- Lines 4–8: Define the inner function. It accesses the enclosing threshold and rate, applies the discount when eligible, and otherwise returns the original amount.
- Line 11: Return the inner function itself, creating a closure rather than executing it immediately.
- Lines 15–16: Create two independent closures with different captured threshold and rate values.
- Lines 20–21: Define sample cart amounts.
- Lines 23–24: Call each closure and format the returned amount to two decimal places.
Test cases
The discount rates and thresholds are hypothetical values for this exercise.