To calculate profit or loss for given cost and selling price
Expt. No.05
Page No.10
Profit or loss
Aim
To write a Python program that accepts the cost price and selling price and displays the profit or loss and its percentage.
Concepts used
Algorithm
- Input the cost price and selling price.
- If SP > CP, profit = SP − CP; profit % = profit ÷ CP × 100.
- If SP < CP, loss = CP − SP; loss % = loss ÷ CP × 100.
- Otherwise there is neither profit nor loss.
Program
# Profit or loss cp = float(input("Cost price : "))sp = float(input("Selling price: "))if sp > cp: profit = sp - cp print("Profit:", profit, " Profit %:", round(profit / cp * 100, 2))elif sp < cp: loss = cp - sp print("Loss:", loss, " Loss %:", round(loss / cp * 100, 2))else: print("No profit, no loss")
A marked line continues the line above it. In your file, write it on the same line.
Output
Run 1
$ python profit_or_loss.py
Cost price : 800
Selling price: 1000
Profit: 200.0 Profit %: 25.0
Run 2
$ python profit_or_loss.py
Cost price : 1200
Selling price: 1050
Loss: 150.0 Loss %: 12.5
✓ Printed by running this program, not typed by hand.
Result
Buying at 800 and selling at 1000 gave a profit of 200.0 (25.0%). Buying at 1200 and selling at 1050 gave a loss of 150.0 (12.5%).
Viva voce: what the examiner may ask
01Why is the percentage calculated on the cost price?
Profit and loss percentages are always worked out on the cost price.
02Which condition handles equal prices?
The final else, since neither sp > cp nor sp < cp is true.
03What does round(x, 2) return?
x rounded to 2 decimal places.
Variations for the lab test
- Find the selling price for a given cost price and profit %.
- Input several items in a loop and find the total profit or loss.
- Display "Good deal" when the profit is above 20%.
! Common mistake Dividing by the selling price instead of the cost price gives the wrong percentage.