To find the sale price of an item with given cost and discount (%)
Expt. No.02
Page No.4
Sale price after discount
Aim
To write a Python program that accepts the cost of an item and the discount percentage and displays the discount amount and the sale price.
Concepts used
Algorithm
- Input the cost and the discount %.
- Discount = cost × discount % ÷ 100.
- Sale price = cost − discount.
- Display both.
Program
# Sale price of an item after discount cost = float(input("Enter the cost of the item: "))disc_percent = float(input("Enter the discount (%) : "))discount = cost * disc_percent / 100sale_price = cost - discountprint("Discount amount:", discount)print("Sale price :", sale_price)
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python sale_price_discount.py
Enter the cost of the item: 2500
Enter the discount (%) : 12
Discount amount: 300.0
Sale price : 2200.0
✓ Printed by running this program, not typed by hand.
Result
A 12% discount on 2500 is 300.0, so the sale price was 2200.0.
Viva voce: what the examiner may ask
01In what order does Python evaluate cost * disc_percent / 100?
Left to right, because * and / have the same precedence.
02What does / always return?
A float, even when the result is a whole number.
03How would you display the price rounded to 2 decimal places?
round(sale_price, 2).
Variations for the lab test
- Give an extra 5% discount if the cost is above 5000.
- Find the discount % when the cost and sale price are given.
- Calculate the price after two successive discounts.
! Common mistake Writing
cost - disc_percent subtracts the percentage number itself (12) instead of 12% of the cost.