Skip to content

Instantly share code, notes, and snippets.

@Katharine
Created March 25, 2012 03:48
Show Gist options
  • Save Katharine/2191250 to your computer and use it in GitHub Desktop.
Save Katharine/2191250 to your computer and use it in GitHub Desktop.
def calc_paid_off(balance, interest_rate, payment):
for month in xrange(12):
balance = round(balance * (1 + interest_rate / 12.0) - payment, 2)
return balance
initial_balance = float(raw_input("Enter the outstanding balance on your credit card: "))
interest_rate = float(raw_input("Enter the annual credit card interest rate as a decimal: "))
print "RESULT"
lower = initial_balance / 12.0
upper = (initial_balance * (1 + (interest_rate / 12.0)) ** 12.0) / 12.0
guess = 0
while True:
guess = round((lower + upper) / 2.0, 2)
result = calc_paid_off(initial_balance, interest_rate, guess)
if abs(result) < 0.12:
print "Monthly payment to pay off debt in 1 year: $%s" % guess
print "Number of months needed: 12"
print "Balance: $%s" % result
break
if result < 0:
upper = guess
elif result > 0:
lower = guess
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment