Default Arguments
Write apply_discount(price, percent=10) that returns the price after taking off a percentage. The default is 10 percent.
apply_discount(200) returns 180.0. apply_discount(200, 25) returns 150.0.
Examples
With no percentage given, the 10 percent default applies
- Input
- apply_discount(200)
- Output
- 180
An explicit percentage overrides the default
- Input
- apply_discount(200, 25)
- Output
- 150
A 0 percent discount leaves the price alone
- Input
- apply_discount(99, 0)
- Output
- 99
Hints
Hint 1
Work directly with the arguments price, percent and return the result rather than printing it.
Hint 2
Return final price rather than discount amount.
Requirements
price: the original pricepercent: the discount percentage, defaulting to 10Return the price after the discount.
Constraints
Standard library only, no imports required
Time limit: 200 ms, Memory: 64 MB
Where this shows up
3 employers weight this skill
2 frontier labs, 1 AI product company. Top match scores 80.
def apply_discount(price, percent=10):
"""
Reduce a price by a percentage.
Args:
price: the original price
percent: the discount percentage, defaulting to 10
Returns:
The price after the discount.
"""
# YOUR CODE HERE
pass