I need to calculate the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. The right answer should be 310, but I get 340. I was editing code for a few hours, yet didn't find any suitable solution. What is wrong here? How is it possible to fix it?
balance = 3329
annualInterestRate = 0.2
payment = 10
def year_balance(init_payment, init_balance):
""" Calculates total debt after 12 months """
interest_sum = 0
for month in range(12):
# balance after monthly payment
unpaid = init_balance - init_payment
# monthly interest of remaining balance
interest = unpaid * (annualInterestRate / 12.0)
interest_sum += interest
yearly = init_balance + interest_sum # total debt after 12 months
return yearly
total = year_balance(payment, balance) # total debt after 12 months
while total - payment * 12 > 0:
# checks if payment is big enough to fully repay the credit after 12 months
payment += 10
print "Lowest payment: ", payment