When someone is quoted a few thousand dollars for dental work, the question that follows is rarely "can I afford this?" It is "what will the monthly payment do to the total?" That is a small piece of arithmetic, and it is a good first project if you are learning Python.
Here is the whole idea in one paragraph: a payment plan is a principal, a rate and a number of months. The monthly payment follows from those three, and the total cost is the monthly payment times the months, plus any fee you pay up front.
The formula
For a loan with a fixed annual rate r and n monthly payments, the payment on principal P is:
def monthly_payment ( principal : float , annual_rate : float , months : int ) -> float : if annual_rate == 0 : return principal / months i = annual_rate / 12 return principal * i / ( 1 - ( 1 + i ) ** - months )
Enter fullscreen mode
Exit fullscreen mode
The zero-rate branch matters.…