Total cost of a subscription plan
A subscription charges a monthly rate for a given number of months. Annual plans get one month free.
- Base price equals monthlyAmount multiplied by months.
- If annual is true, subtract one month from the total.
- months of 0 or fewer returns 0.
subscription_total(monthly_amount: int, months: int, annual: bool) → int
Where you start
def subscription_total(monthly_amount: int, months: int, annual: bool) -> int:
Worked examples
| Call | Result |
|---|---|
subscription_total(1000, 12, True) | 11000 |
subscription_total(1000, 12, False) | 12000 |
subscription_total(500, 6, True) | 2500 |
subscription_total(0, 12, False) | 0 |
Hint
Multiply first, then conditionally subtract.
Reference solution in Python
def subscription_total(monthly_amount: int, months: int, annual: bool) -> int:
if months <= 0:
return 0
total = monthly_amount * months
if annual:
total -= monthly_amount
return total