Drill

ProblemsPython › billing

Total cost of a subscription plan

mediumbillingPython

A subscription charges a monthly rate for a given number of months. Annual plans get one month free.

subscription_total(monthly_amount: int, months: int, annual: bool) → int

Solve it in the editor →

Where you start

def subscription_total(monthly_amount: int, months: int, annual: bool) -> int:
    

Worked examples

CallResult
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

The same problem in another language

More billing problems in Python