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:
- number of shirts,
n - price of one shirt,
price - discount percent,
d
total = n * pricediscount = total * d / 100payable = total - discount
- total before discount
- discount amount
- amount to pay
2The program
# 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)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:
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:
total = 1497.0
d = 10
discount = total * d / 100
print(discount)149.7
% 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:
total = 1497.0
d = 10
payable = total * (100 - d) / 100
print(payable)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
- Add GST of 5% to the payable amount and print the final total.
- Print how much is saved per shirt (
discount / n). - Run it with a discount of 0 and of 100. Does the bill still make sense?
5Recap
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.Which expression gives 10% of total?
3 shirts at ₹499 with 10% off. What is the amount to pay?