A small Python module that calculates the final price after applying a percentage discount. It exposes a single reusable function, apply_discount, which validates its inputs and returns either the discounted price or a human-readable error message.
| File | Description |
|---|---|
main.py |
Defines the apply_discount function. |
LICENSE |
CC0 1.0 Universal public domain dedication. |
.gitignore |
Standard Python ignore rules for version control. |
-
Make sure you have Python 3 installed.
-
Import and call the function from your own code, or test it directly:
python -c "from main import apply_discount; print(apply_discount(100, 20))"
The apply_discount function takes a price and a discount (both expected to
be numbers) and returns the final price after the discount is applied.
def apply_discount(price, discount):
if type(price) not in [int, float]:
return "The price should be a number"
if type(discount) not in [int, float]:
return "The discount should be a number"
if price <= 0:
return "The price should be greater than 0"
if discount < 0 or discount > 100:
return "The discount should be between 0 and 100"
discount_amount = price * (discount / 100)
final_price = price - discount_amount
return final_priceBefore computing anything, the function checks the inputs and returns an error string when a rule is violated:
pricemust be anintorfloat, otherwise"The price should be a number".discountmust be anintorfloat, otherwise"The discount should be a number".pricemust be greater than0, otherwise"The price should be greater than 0".discountmust be between0and100inclusive, otherwise"The discount should be between 0 and 100".
When all checks pass, it computes discount_amount = price * (discount / 100)
and returns final_price = price - discount_amount.
from main import apply_discount
print(apply_discount(100, 20)) # 80.0
print(apply_discount(50, 10)) # 45.0
print(apply_discount(100, 0)) # 100.0
print(apply_discount(100, 100)) # 0.0
print(apply_discount("100", 20)) # The price should be a number
print(apply_discount(100, 150)) # The discount should be between 0 and 100Note: the module itself only defines the function — it has no top-level
python main.pyproduces no output. Import it as shown above to use it.
- Define a function with parameters and a
returnvalue. - Validate inputs with guard clauses that
returnearly on bad data. - Use
type()and a list membership check (in [...]) to accept multiple numeric types. - Compute a percentage-based discount and return a clean result.
Released under CC0 1.0, placing the work in the public domain.