Introduction to Python

Activity 1: Profit & Loss

Lesson 3: Algorithms

In this activity, we will design an algorithm and flowchart first, then write a Python program that calculates whether a transaction results in a profit, a loss, or breaks even.

Illustration showing a shopkeeper with price tags, a green arrow going up for profit and a red arrow going down for loss

Understanding Profit & Loss

Profit & Loss Formulas

ConditionFormula
ProfitSelling Price > Cost Price → Profit = Selling Price - Cost Price
LossSelling Price < Cost Price → Loss = Cost Price - Selling Price
Break EvenSelling Price = Cost Price → No profit, no loss

Algorithm

  • START
  • INPUT: Ask the user for the Cost Price (CP)
  • INPUT: Ask the user for the Selling Price (SP)
  • DECISION: Is SP greater than CP?
  • YES → Calculate Profit = SP - CP, OUTPUT: Print profit amount
  • NO → DECISION: Is SP less than CP?
  • YES → Calculate Loss = CP - SP, OUTPUT: Print loss amount
  • NO → OUTPUT: Print 'Break Even — no profit or loss'
  • END
Flowchart for the profit and loss calculator showing start, two input boxes for CP and SP, a diamond decision for SP greater than CP, branches to profit calculation and another decision for SP less than CP, then loss calculation or break even output, and end

Writing the Python Code

# Profit and Loss Calculator cp = float(input("Enter the Cost Price: ")) sp = float(input("Enter the Selling Price: ")) if sp > cp: profit = sp - cp print("Profit: " + str(profit)) elif sp < cp: loss = cp - sp print("Loss: " + str(loss)) else: print("Break Even — no profit or loss")

Understanding the Code

Code Breakdown

LineWhat it Does
float(input(...))Gets a decimal number from the user
if sp > cpChecks if selling price is greater than cost price
profit = sp - cpCalculates the profit amount
elif sp < cpChecks if selling price is less than cost price (only if first condition was false)
loss = cp - spCalculates the loss amount
elseRuns if neither condition was true — break even case
str(profit)Converts the number to a string so it can be joined with text

Testing the Program

  • Run the program and enter CP = 500, SP = 700 → Should print Profit: 200.0
  • Run again with CP = 800, SP = 600 → Should print Loss: 200.0
  • Run again with CP = 500, SP = 500 → Should print Break Even
Python console output showing three test runs of the profit and loss calculator with profit, loss, and break even results
Save your file with the name Profit and Loss Calculator before moving on!

Subscribe to our newsletter.

Get updates to news and events.