gpt4 book ai didi

python - 信用计算器产生错误的输出

转载 作者:行者123 更新时间:2023-12-01 04:53:29 26 4
gpt4 key购买 nike

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

最佳答案

您实际上并不需要迭代来计算每月还款额。相反,您可以使用封闭式解决方案:

loan_amount = 3329
annual_interest_rate = 0.2
monthly_repayment = ((loan_amount * annual_interest_rate / 12.) /
(1 - (1 + annual_interest_rate / 12.) ** -12))
print monthly_repayment

这假设每月还款和每月复利。对于一般公式,请参见,例如 here .

关于python - 信用计算器产生错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27952819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com