Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Discount Calculator

Calculate a discount and payable price with Python. Students convert a percentage, subtract its monetary value, and display original, saved, and final amounts.

BeginnerPhase 6 - Category 5: Real-World Logic ProblemsExample 9 of 15
discount-calculator.py
1# Take inputs
2price = float(input("Enter price: "))
3discount_percent = float(input("Enter discount (%): "))
4
5# Calculate
6discount_amount = (price * discount_percent) / 100
7final_price = price - discount_amount
8
9print(f"Original price: {price:.2f}")
10print(f"Discount ({discount_percent}%): {discount_amount:.2f}")
11print(f"Final price: {final_price:.2f}")

Output

Enter price: 1000
Enter discount (%): 20
Original price: 1000.00
Discount (20.0%): 200.00
Final price: 800.00

In this one

  • Convert a percent reduction
  • Subtract the monetary discount
  • Keep monetary quantities separate

How it works

  1. 1

    Read price and discount_percent.

  2. 2

    Calculate price * discount_percent / 100.

  3. 3

    Subtract that amount and display all three values.

Edge cases

What does a zero-percent discount do?

What does a zero-percent discount do?

It leaves the final price equal to the original.

Does the code reject discounts above 100?

Does the code reject discounts above 100?

No. Such input can produce a negative final price.

What's going on

discount_amount is price * discount_percent / 100. Subtracting it from price produces final_price.

For a price of one hundred and a ten-percent discount, the reduction is ten and the result ninety. .2f controls displayed decimals only. The example accepts any numeric percent, so negative or above-one-hundred discounts can produce unusual results without validation.

What does a zero-percent discount do? It leaves the final price equal to the original.

Does the code reject discounts above 100? No. Such input can produce a negative final price.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What is subtracted from price?

Pick an answer to check it.

Question 2

What does a zero-percent discount do?

Pick an answer to check it.

Questions people ask

What does a zero-percent discount do?

It leaves the final price equal to the original.

Does the code reject discounts above 100?

No. Such input can produce a negative final price.

What is subtracted from price?

The calculated discount amount.