Complete Python Series · Part 23 of 29
Assignment 10-delivery-cost
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 Course Certificate Delivery Calculator
Build a reusable function to calculate certificate delivery costs using type hints, default parameters, keyword arguments, docstrings and return values.
Problem statement
NextGen CTO offers physical copies of course completion certificates. Students can choose standard delivery at INR 50 per certificate or express delivery at INR 75 per certificate. These are hypothetical exercise prices. Calculate the total delivery cost based on the quantity and delivery option.
Task requirements
- Define
calculate_delivery_cost(quantity: int, express: bool = False) -> float. - Write a docstring describing the function, parameters and return value.
- Set the standard price to 50.0 per certificate.
- Use 75.0 per certificate when express delivery is selected.
- Return the total cost and print the supplied test results.
Constraints
- Assume non-negative integer quantities and Boolean express arguments.
- Use a default argument for standard delivery.
- Return a float rather than printing inside the function.
- Use standard Python without external libraries.
Good luck trying the exercise!
Starter code
def calculate_delivery_cost(
quantity: int, express: bool = False
) -> float:
# TODO: Write your docstring
# TODO: Set the standard delivery price
# TODO: Update the price for express delivery
# TODO: Calculate and return the total cost
pass
# Test cases
print(f"Standard (10 certificates): INR {calculate_delivery_cost(10)}")
print(f"Express (5 certificates): INR {calculate_delivery_cost(5, express=True)}")
Hints
- Use triple quotes for the docstring immediately after the function signature.
quantity: intandexpress: booldescribe expected argument types.express=Falsemakes standard delivery the default.- Use an
ifstatement to select the express price. - Multiply the quantity by the selected price and use
return.
Expected output
Standard (10 certificates): INR 500.0
Express (5 certificates): INR 375.0
Solution: NextGen CTO Course Certificate Delivery Calculator
The solution defines a documented, type-hinted function that chooses a delivery rate and returns the calculated cost. The caller can override the default delivery option using a keyword argument.
def calculate_delivery_cost(
quantity: int, express: bool = False
) -> float:
"""Calculate the delivery cost of physical course certificates.
Args:
quantity: Number of certificates to deliver.
express: Whether express delivery is required.
Defaults to False.
Returns:
Total delivery cost in INR.
"""
cost_per_certificate = 50.0
if express:
cost_per_certificate = 75.0
return quantity * cost_per_certificate
# Test cases
print(f"Standard (10 certificates): INR {calculate_delivery_cost(10)}")
print(f"Express (5 certificates): INR {calculate_delivery_cost(5, express=True)}")
Solution explanation
- Lines 1–3: Define the function with parameter type hints, a default Boolean argument and a float return type hint. Type hints do not enforce types at runtime.
- Lines 4–13: Document the purpose, arguments and return value using a docstring.
- Line 14: Set the standard rate to 50.0.
- Lines 16–17: Override the rate when express delivery is requested.
- Line 19: Multiply quantity by the rate and return the total.
- Lines 23–24: Call the function using the default option and an explicit keyword argument, then print the results.