我需要計算在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