LambdaLabTM
Computer Science · Class 11 · Sample Programs
Sample programPercentages⏱️ 7 min read

Cost of n Shirts with a Discount

The problem: a shop sells shirts. Ask how many the customer is buying, the price of one shirt, and the discount percent. Print the bill: full amount, discount given, and the amount to pay.

1Plan it first

Three inputs this time, and three lines of working. Writing the working out as separate steps means each one can be printed and checked:

Input
what we ask the user for
  • number of shirts, n
  • price of one shirt, price
  • discount percent, d
Process
what we work out
  • total = n * price
  • discount = total * d / 100
  • payable = total - discount
Output
what we show
  • total before discount
  • discount amount
  • amount to pay

2The program

shirt_bill.py
# bill for n shirts with a discount

n = int(input('How many shirts? '))
price = float(input('Price of one shirt: '))
d = float(input('Discount percent: '))

total = n * price
discount = total * d / 100
payable = total - discount

print('Total before discount =', total)
print('Discount =', discount)
print('Amount to pay =', payable)
Output
How many shirts? 3
Price of one shirt: 499
Discount percent: 10
Total before discount = 1497.0
Discount = 149.7
Amount to pay = 1347.3

int() for the count — nobody buys 2.5 shirts — float() for the money and the percent. Run it and change the numbers:

shirt_bill.py

3What a percent actually is

“10 percent” means 10 out of every 100. There is no percent sign in the maths — you divide by 100:

percent.py
total = 1497.0
d = 10

discount = total * d / 100

print(discount)
Output
149.7
% is not percent in Python
Python does have a % operator, but it is modulus — the remainder after division. total * d % 100 would give you something meaningless. To take a percentage, always * d / 100.

There is a shorter way to reach the same answer. Paying after a 10% discount is paying 90% of the bill, so you can skip the middle step:

shorter.py
total = 1497.0
d = 10

payable = total * (100 - d) / 100

print(payable)
Output
1347.3

Same answer. The longer version is still the better program here, because the customer wants to see how much they saved — and a bill that shows its working is easier to trust.

4Now you try

  1. Add GST of 5% to the payable amount and print the final total.
  2. Print how much is saved per shirt (discount / n).
  3. Run it with a discount of 0 and of 100. Does the bill still make sense?
your_turn.py

5Recap

Key Takeaway
A percentage in code is amount * percent / 100 — never the % operator, which means remainder. Use int() for counts and float() for money. Breaking the bill into total, discount and payable lets you print each line and spot a wrong one instantly.
Quick Check

Which expression gives 10% of total?

Quick Check

3 shirts at ₹499 with 10% off. What is the amount to pay?