Exercise: Find Discount


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.

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:

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:

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:

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:

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:

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: