我需要计算所需的最低每月固定付款额,以便在12个月内还清信用卡余额。正确的答案应该是310,但我得到340。我正在编辑代码几个小时,但没有找到合适的解决方案。怎么了如何解决?
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
最佳答案
我们需要为每个新的付款金额运行余额计数功能,因为对于大笔付款,利息金额会较小。所以
balance = 3329
annualInterestRate = 0.2
payment = 10
def year_balance(init_payment, init_balance):
""" Calculates total debt after 12 months """
interest_sum = 0
unpaid = init_balance
for month in range(12):
# balance after monthly payment
unpaid -= init_payment
# monthly interest of remaining balance
unpaid += unpaid * (annualInterestRate / 12.0)
return unpaid
while year_balance(payment, balance) > 0:
payment += 10
print("Lowest payment: ", payment)
注意:这种情况是为了防止您在信用期开始之前付款。如果一个月后完成了此操作,则应首先添加每月利率。