Calculating Profit or Loss for a Fruit Seller: A Simple Python Guide

If you’re a beginner looking to understand how to calculate profit or loss using Python, you’re in the right place! In this blog, we’ll guide you through a simple Python program that helps a fruit seller determine whether they’ve made a profit or incurred a loss after selling a dozen bananas.

Problem Statement

A fruit seller buys a dozen bananas at a certain cost and then sells each banana at a different price. We need to calculate whether the seller has made a profit or a loss. If there’s no difference between the cost and the selling price, we’ll note that there is no profit or loss.

Step-by-Step Explanation

1. Define the Function to Calculate Profit or Loss

The core part of our program is the calculate_profit_loss function. Here’s how it works:

def calculate_profit_loss(total_cost, selling_price_per_banana):
    total_selling_price = selling_price_per_banana * 12
    profit_or_loss = total_selling_price - total_cost
    return profit_or_loss
  • total_cost: The total cost of buying a dozen bananas.
  • selling_price_per_banana: The price at which each banana is sold.
  • total_selling_price = selling_price_per_banana * 12: Calculate the total revenue by multiplying the selling price per banana by 12 (since there are 12 bananas in a dozen).
  • profit_or_loss = total_selling_price - total_cost: Calculate the profit or loss by subtracting the total cost from the total selling price.
  • return profit_or_loss: The function returns the profit or loss value.

2. Taking Input from the User

Next, we’ll take two floating-point numbers as input from the user:

  • The total cost (x) of a dozen bananas.
  • The selling price per banana (y).

We use the input() function combined with split() to allow the user to input both values in a single line:

x, y = map(float, input("Enter x and y: ").split())
  • map(float, ...): Converts the input values to floating-point numbers.
  • split(): Splits the input string into separate values based on spaces.

3. Calculate and Display Profit or Loss

After gathering the input, we calculate the profit or loss using the calculate_profit_loss function. Then, we check if the result is positive, negative, or zero:

result = calculate_profit_loss(x, y)
if result > 0:
    print(f"The fruit seller made a profit of Rs. {result:.2f}")
elif result < 0:
    print(f"The fruit seller incurred a loss of Rs. {abs(result):.2f}")
else:
    print("There is no profit or loss.")
  • if result > 0:: If the result is positive, the seller made a profit.
  • elif result < 0:: If the result is negative, the seller incurred a loss. We use abs() to display the absolute value.
  • else:: If the result is zero, there’s no profit or loss.

Full Code

Here’s the complete Python program:

def calculate_profit_loss(total_cost, selling_price_per_banana):
    total_selling_price = selling_price_per_banana * 12
    profit_or_loss = total_selling_price - total_cost
    return profit_or_loss

x, y = map(float, input("Enter x and y: ").split())
result = calculate_profit_loss(x, y)

if result > 0:
    print(f"The fruit seller made a profit of Rs. {result:.2f}")
elif result < 0:
    print(f"The fruit seller incurred a loss of Rs. {abs(result):.2f}")
else:
    print("There is no profit or loss.")

Test Cases

Let’s test our program with some examples to see how it works.

Test Case 1:

  • Input:
  • Total cost (x): 30.00
  • Selling price per banana (y): 3.00
  • Expected Output:
  • The fruit seller made a profit of Rs. 6.00

Explanation:
Total Selling Price = 3.00 * 12 = Rs. 36.00
Profit = 36.00 – 30.00 = Rs. 6.00

Test Case 2:

  • Input:
  • Total cost (x): 36.00
  • Selling price per banana (y): 2.50
  • Expected Output:
  • The fruit seller incurred a loss of Rs. 6.00

Explanation:
Total Selling Price = 2.50 * 12 = Rs. 30.00
Loss = 36.00 – 30.00 = Rs. 6.00

Test Case 3:

  • Input:
  • Total cost (x): 24.00
  • Selling price per banana (y): 2.00
  • Expected Output:
  • There is no profit or loss.

Explanation:
Total Selling Price = 2.00 * 12 = Rs. 24.00
Profit/Loss = 24.00 – 24.00 = Rs. 0.00

Conclusion

This simple Python program is a great way to start learning about basic arithmetic operations, conditional statements, and functions in Python. By following the steps outlined in this blog, you can now easily determine whether a fruit seller has made a profit or incurred a loss based on their buying and selling prices. Happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top