If you’re running a newspaper agency and want to calculate the profit you make each Sunday, this simple Python program is just what you need. In this guide, we’ll walk you through the process step-by-step. You’ll learn how to write a Python program that takes into account the number of copies sold, the selling price, and the cost price of each newspaper to calculate the profit.
What Does the Program Do?
This program calculates the profit by considering the following factors:
- Number of copies sold (
x
). - Selling price per copy (
a
). - Cost price per copy (
b
). - Fixed costs of Rs. 100, which cover expenses like storage, delivery, etc.
Step-by-Step Explanation
1. Define the Function to Calculate Profit
The heart of this program is a function called calculate_profit
. Here’s the breakdown of how it works:
def calculate_profit(x, a, b):
fixed_cost = 100
revenue = x * a
total_cost = (x * b) + fixed_cost
profit = revenue - total_cost
return profit
fixed_cost = 100
: We start by setting the fixed cost to Rs. 100. This is a constant amount spent every Sunday.revenue = x * a
: Next, we calculate the revenue generated by multiplying the number of copies sold (x
) by the selling price per copy (a
).total_cost = (x * b) + fixed_cost
: The total cost is then calculated by adding the cost of purchasing the newspapers (x * b
) to the fixed cost.profit = revenue - total_cost
: Finally, the profit is determined by subtracting the total cost from the revenue.return profit
: The function returns the profit value.
2. Taking Input from the User
The next part of the code takes input from the user for the number of copies sold, the selling price, and the cost price.
x = int(input("Enter the number of copies sold: "))
a = int(input("Enter the selling price per copy: "))
b = int(input("Enter the cost price per copy: "))
Here, int(input(...))
is used to prompt the user for input, which is then converted to an integer so it can be used in calculations.
3. Calculate and Display the Profit
After the input is collected, we use the calculate_profit
function to compute the profit:
profit = calculate_profit(x, a, b)
print(profit)
The result is then printed, showing the profit made by the newspaper agency on that Sunday.
Full Code
Here’s the complete Python program:
def calculate_profit(x, a, b):
fixed_cost = 100
revenue = x * a
total_cost = (x * b) + fixed_cost
profit = revenue - total_cost
return profit
x = int(input("Enter the number of copies sold: "))
a = int(input("Enter the selling price per copy: "))
b = int(input("Enter the cost price per copy: "))
profit = calculate_profit(x, a, b)
print(profit)
Test Cases
Let’s test this program with a few examples to see how it works.
Test Case 1:
- Input:
- Number of copies sold (
x
): 150 - Selling price per copy (
a
): 12 - Cost price per copy (
b
): 8 - Expected Output: 500
Explanation:
Revenue = 150 * 12 = Rs. 1800
Total Cost = (150 * 8) + 100 = Rs. 1300
Profit = 1800 – 1300 = Rs. 500
Test Case 2:
- Input:
- Number of copies sold (
x
): 100 - Selling price per copy (
a
): 15 - Cost price per copy (
b
): 10 - Expected Output: 400
Explanation:
Revenue = 100 * 15 = Rs. 1500
Total Cost = (100 * 10) + 100 = Rs. 1100
Profit = 1500 – 1100 = Rs. 400
Test Case 3:
- Input:
- Number of copies sold (
x
): 200 - Selling price per copy (
a
): 20 - Cost price per copy (
b
): 18 - Expected Output: 300
Explanation:
Revenue = 200 * 20 = Rs. 4000
Total Cost = (200 * 18) + 100 = Rs. 3700
Profit = 4000 – 3700 = Rs. 300
Conclusion
With this simple Python program, you can easily calculate the profit your newspaper agency makes on any given Sunday. All you need to do is enter the number of copies sold, the selling price per copy, and the cost price per copy. The program will then compute the profit after accounting for all costs. This is a great way to get started with basic programming and understand how functions and calculations work in Python!