我需要计算在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
您提供的代码给出了一个错误 - 是否将'interest_total = 0'更改为'interest_sum = 0'? –
看来您每次调整付款时都没有重新计算总额。由于您的代码按月收取利息,您应该在每次付款更改时重新计算总额。 – gankoji
@DavidRobinson是的,我修改了代码 – Dovi