Start learning free

LESSON 18 OF 410 · CALCULATIONS WITH NUMBERS

Exercise: Find Discount

Lesson Read the full explanation

We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the discount amount in dollars.


Example:

For example, if price was 150 and discount was 10, the answer would be 15.

Why? Because the discount is 10%. And 10% of 150 dollars is 15 dollars.

STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · CALCULATIONS WITH NUMBERS

    Find Discount in Python, explained

    Introduction

    In this lesson, we will learn how to calculate the discount amount given the original price of a product and the discount percentage. This is a common task in various applications, such as e-commerce websites, where discounts are frequently applied to products.

    Understanding how to perform this calculation is essential for anyone working with financial data or developing applications that involve pricing and discounts.

    Understanding the Basics

    Before diving into the solution, let's understand the basic concepts:

    • Price: The original cost of the product before any discounts are applied.
    • Discount Percentage: The percentage by which the original price is reduced.
    • Discount Amount: The actual amount of money that is subtracted from the original price.

    To calculate the discount amount, we use the formula:

    discount_amount = (price * discount) / 100

    This formula multiplies the original price by the discount percentage and then divides by 100 to get the discount amount in dollars.

    Main Concepts

    Let's break down the key concepts and techniques involved:

    • Arithmetic Operations: We will use multiplication and division to calculate the discount amount.
    • Variables: We will store the price and discount percentage in variables for easy manipulation.

    Here is the step-by-step approach to solve the problem:

    1. Store the original price in a variable.
    2. Store the discount percentage in another variable.
    3. Use the formula to calculate the discount amount.
    4. Print the discount amount.

    Examples and Use Cases

    Let's look at a few examples to understand how this works:

    Example 1:

    price = 150
    discount = 10
    discount_amount = (price * discount) / 100
    print(discount_amount)  # Output: 15

    In this example, the original price is $150, and the discount percentage is 10%. The discount amount is $15.

    Example 2:

    price = 200
    discount = 25
    discount_amount = (price * discount) / 100
    print(discount_amount)  # Output: 50

    In this example, the original price is $200, and the discount percentage is 25%. The discount amount is $50.

    Common Pitfalls and Best Practices

    Here are some common mistakes to avoid and best practices to follow:

    • Incorrect Formula: Ensure you use the correct formula to calculate the discount amount.
    • Data Types: Make sure the price and discount percentage are numerical values.
    • Code Readability: Use meaningful variable names and add comments to make your code easy to understand.

    Advanced Techniques

    For more advanced scenarios, you might want to handle cases where the discount percentage is not a whole number or where additional discounts are applied. Here is an example:

    price = 250.75
    discount = 12.5
    discount_amount = (price * discount) / 100
    print(discount_amount)  # Output: 31.34375

    In this example, the original price is $250.75, and the discount percentage is 12.5%. The discount amount is $31.34 (rounded to two decimal places).

    Code Implementation

    Here is the complete code implementation:

    # Define the original price and discount percentage
    price = 150
    discount = 10
    
    # Calculate the discount amount
    discount_amount = (price * discount) / 100
    
    # Print the discount amount
    print(discount_amount)  # Output: 15

    Debugging and Testing

    When debugging and testing your code, consider the following tips:

    • Print Statements: Use print statements to check the values of variables at different stages.
    • Edge Cases: Test with edge cases, such as a 0% discount or a 100% discount.
    • Automated Tests: Write automated tests to verify the correctness of your code.

    Here is an example of a simple test case:

    def test_discount():
        assert (150 * 10) / 100 == 15
        assert (200 * 25) / 100 == 50
        assert (250.75 * 12.5) / 100 == 31.34375
    
    test_discount()

    Thinking and Problem-Solving Tips

    When approaching problems related to discounts, consider the following strategies:

    • Break Down the Problem: Divide the problem into smaller, manageable parts.
    • Use Simple Examples: Start with simple examples to understand the basic concept.
    • Practice: Solve similar problems to improve your understanding and skills.

    Conclusion

    In this lesson, we learned how to calculate the discount amount given the original price and discount percentage. We covered the basic concepts, provided examples, discussed common pitfalls, and shared best practices. Mastering these concepts is essential for anyone working with financial data or developing applications involving pricing and discounts.

    Keep practicing and exploring further applications to strengthen your understanding.

    Additional Resources

    Here are some additional resources to help you further:

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free