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

标签 python python-2.7 calculator currency

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

相关文章:

python - 制作变量的本地别名会像循环一样加快列表理解吗?

python - Pyodbc 连接错误但 isql 工作

python - 如何仅绘制具有数组某些值的某些单元格,而其他单元格则不使用 matplotlib.pyplot?

python - 彩色图像的直方图均衡使用python opencv给出错误

Python2 : iterable inside sum inside list inside class causes error

ios - 如何复制两个标签的结果?

python - 查找与 pandas 中的条件匹配的第一行的索引

python - 将空白分隔数字列表转换为 float 列表

java - 在 fragment 中显示默认计算器

java - .class错误无法弄清楚