Bird
0
0

Identify the issue in this Python code snippet for applying a discount:

medium📝 Analysis Q6 of 15
LLD - Design — Online Shopping Cart
Identify the issue in this Python code snippet for applying a discount:
price = 120
discount = 15
final_price = price - (price * discount)
print(final_price)
ADiscount should be divided by 100 to represent percentage
BPrice should be multiplied by discount without parentheses
CDiscount variable should be a string
DFinal price calculation is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze discount calculation

    The discount is used as 15 instead of 0.15, causing an incorrect large subtraction.
  2. Step 2: Correct approach

    Discount percentage must be divided by 100 to convert to decimal.
  3. Final Answer:

    Discount should be divided by 100 to represent percentage -> Option A
  4. Quick Check:

    Percentage discounts require decimal conversion [OK]
Quick Trick: Divide percentage discount by 100 before multiplying [OK]
Common Mistakes:
  • Using discount as whole number instead of decimal
  • Ignoring operator precedence in calculations
  • Assuming discount variable must be string

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes