Write a Python Program to Calculate Profit or Loss with a practical example.
Python Program to Calculate Profit or Loss using Elif Statement
This python program allows the user to enter the Sales amount and Actual cost of a Product. Next, Python calculates the Loss Amount or profit Amount based on those two values using Elif Statement.
# Python Program to Calculate Profit or Loss actual_cost = float(input(" Please Enter the Actual Product Price: ")) sale_amount = float(input(" Please Enter the Sales Amount: ")) if(actual_cost > sale_amount): amount = actual_cost - sale_amount print("Total Loss Amount = {0}".format(amount)) elif(sale_amount > actual_cost): amount = sale_amount - actual_cost print("Total Profit = {0}".format(amount)) else: print("No Profit No Loss!!!")
Let me try different values
if(actual_cost > sale_amount): amount = actual_cost - sale_amount print("Total Loss Amount = {0}".format(amount)) elif(sale_amount > actual_cost): amount = sale_amount - actual_cost print("Total Profit = {0}".format(amount)) else: print("No Profit No Loss!!!")
- If condition checks whether the actual cost is greater than the sale amount. If True, then Python prints the loss amount as actual cost – sales amount
- Elif statement checks whether the sale amount is greater than the actual cost. If True, it prints the profit amount as sales amount – actual cost
- If the above conditions fail, it means No Profit No Loss.
Python Program to find Profit or Loss using Arithmetic Operator
In this profit or loss python program, we are using a Minus operator.
# Python Program to Calculate Profit or Loss actual_cost = float(input(" Please Enter the Actual Product Price: ")) sale_amount = float(input(" Please Enter the Sales Amount: ")) if(actual_cost - sale_amount > 0): amount = actual_cost - sale_amount print("Total Loss Amount = {0}".format(amount)) elif(sale_amount - actual_cost > 0): amount = sale_amount - actual_cost print("Total Profit = {0}".format(amount)) else: print("No Profit No Loss!!!")