这个程序运行得很好,但它返回的每月付款是完全不可用的。如果本金为400000美元,利率为11%,付款期为10年,则每月返回44000.16美元。我在谷歌上搜索了方程(算法?)我不知道我哪里出错了。在
import locale
locale.setlocale(locale.LC_ALL, '')
def mortgage(principal, interest, n):
payment = principal*((interest*(1+interest)**n) / ((1+interest)**n-1))
return payment
principal = float(input("What is the amount of the loan you are taking out? $"))
interest = float(input("What is the interest rate? (%) ")) / 100
n = float(input("How many years? ")) * 12
print
print "Your monthly payment would be", locale.currency(mortgage(principal, interest, n))
问题在于你使用的利率。你要求的是年利率,而不是月利率。在
来自https://en.wikipedia.org/wiki/Mortgage_calculator#Monthly_payment_formula:
我刚在我的电脑上试过,把利率除以12,计算出5510美元/月,这与其他抵押贷款计算器是一致的。在
相关问题 更多 >
编程相关推荐