# Author:
# Date: 5/9/25
# Exam 2, Problem 2 (10 out of 20 points)

""" In this program you'll write a function that calculates the number of months
it will take someone to pay off a loan. The loan and payment process works as
follows:
 - At the beginning of Month 0, you loan someone an amount - call it A - of
   money (or cheese, or whatever they're short on). This amount is the starting
   balance - let's call it B. For example, suppose you lend me 10 slices of
   cheese, so B=10 to start.
 - After 1 month and every month thereafter until the loan is
   paid off, your debtor (that's the person you lent the cheese to) owes you
   B * R interest, where R is the interest rate. They pay you a monthly payment
   amount P, which (hopefully, for their sake) covers the entire interest
   payment   and some of the pre-existing balance.
 - This repeats for every month until the balance reaches zero.
   
Here's an example:
 - You loan me 10 slices of gouda at an interest rate of 10%.
 - After one month, the balance becomes 10 * .10 = 11.
   I give you back 3 slices of gouda, so my balance goes to 8.
 - After two months, I owe 8 * .10 = 8.8. I give you 3 slices,
   so now my balance is 5.8
 - This repeats until my balance is zero; in this case, it be
   paid off after 5 months:
      months elapsed   balance
             1         8.0
             2         5.8
             3         6.38
             4         0.718
             5         0.0 (the last payment needed only 0.7898 to reach 0)
             
You have two tasks:
 1. Implement the payoff_months function below.
 2. Write a main program inside a main guard that reads three floats
    (loan_amount, interest_rate, and payment) as command line arguments,
    and prints the result of payoff_months with those inputs.
    
Example outputs:


"""
import sys

def payoff_months(loan_amount, interest_rate, payment):
    """ Return the number of months it will take to pay off a loan.
     - loan_amount is the starting balance
     - interest_rate is the fraction of the current balance due each month
       (e.g., 0.05 would be an interest rate of 5%)
     - payment is the monthly payment amount
    If the loan would never be paid off, return -1
    Precondition: all inputs are non-negative. """
    
    months = 0
    balance = loan_amount
    while balance > 0:
        months += 1
        interest_owed = balance * interest_rate
        if interest_owed >= payment:
            return -1
        balance = balance + interest_owed
        balance = balance - payment

    return months

# print(__name__)

if __name__ == "__main__":
    balance = float(sys.argv[1])
    rate = float(sys.argv[2])
    payment = float(sys.argv[3])
    print(payoff_months(balance, rate, payment))
