Raise on Invalid Discount

Easy ⏱ 8 min 82% acceptance ★★★★★ 4.6
Write apply_discount(price, pct) that returns the discounted price — but raises ValueError with the message 'pct must be between 0 and 100' when pct is outside that range. Validating inputs by raising (not returning error codes) is the Pythonic contract.

Examples

Example 1
Input
apply_discount(200, 25)
Output
150.0
Explanation

25% off 200.

Example 2
Input
apply_discount(200, 150)
Output
ValueError: pct must be between 0 and 100
Explanation

Out-of-range percentage.

Constraints

  • Raise ValueError with that exact message.
  • Valid range is 0-100 inclusive.

Topics

Exception Handlingraise

Companies

RazorpayPayPalFlipkart

Hints

Hint 1

raise ValueError('...') aborts the function.

Hint 2

Check before computing.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.