Module calculate_discount.calc

Module for discount calculation logic.

Functions

def calculate_discount(amount, discount_percent)
Expand source code
def calculate_discount(amount, discount_percent):
    """
    Calculate the final price after applying a discount.

    Args:
        amount (float): The original purchase amount. Must be non-negative.
        discount_percent (float): The discount percentage to apply. Must be between 0 and 100 inclusive.

    Returns:
        float: The final price after subtracting the discount amount.

    Raises:
        ValueError: If amount is negative or discount_percent is outside the range [0, 100].
    """
    if amount < 0:
        raise ValueError("Сумма не может быть отрицательной")
    if discount_percent < 0 or discount_percent > 100:
        raise ValueError("Процент скидки должен быть от 0 до 100")
    discount_amount = amount * (discount_percent / 100)
    final_price = amount - discount_amount
    return final_price

Calculate the final price after applying a discount.

Args

amount : float
The original purchase amount. Must be non-negative.
discount_percent : float
The discount percentage to apply. Must be between 0 and 100 inclusive.

Returns

float
The final price after subtracting the discount amount.

Raises

ValueError
If amount is negative or discount_percent is outside the range [0, 100].