这是我正在处理的问题
这是我的代码
p = int(input('Enter the amount of the loan: '))
r = float(input('Enter the interest rate: '))
d = int(input('Enter the length of loan in months: '))
if p > 0 and r > 0 and d > 0:
m_payment = (p*r)/(1-(1+r) ** 1/d)
t_interest = d*m_payment-p
else:
print('Enter a postive number')
print('Monthly payment: ${0:.2f}'.format( m_payment))
print('Total interest paid: ${0:.2f}'.format(t_interest))
我是否正确输入了指数?
答案 0 :(得分:2)
1)公式使用n
,而不是d
,为什么不使用n
?
无论如何......
我是否正确输入了指数?
您是否获得了如图所示的输出?您可以使用普通计算器来验证这些数字。
话虽如此,如果你仔细阅读这个问题,你最初输入的是“年度利息”,而不是r
。说明书指出r
是年度利息超过12岁。
所以这是对代码的调整。
p = annual_interest = n = -1
while True:
p = int(input('Enter the amount of the loan: '))
annual_interest = float(input('Enter the interest rate: '))
n = int(input('Enter the length of loan in months: '))
if p <= 0 or annual_interest <= 0 or n <= 0:
print('Enter a postive number')
else:
break
r = annual_interest / (12 * 100) # this needs to be a decimal percentage
然后,1/n
不是负指数,它是第N个根。你可以在Python中放一个负指数,不需要试着认为你可以用不同的方式重写它。
monthly_payment = (p * r) / (1 - ((1 + r) ** -n))
interest = n * monthly_payment - p
答案 1 :(得分:0)
没有。你没有正确使用指数。 该公式明确指出您必须使用否定指数,但您正在尝试获取dth-root。
答案 2 :(得分:0)
这个问题似乎只是在处理年度利率&#34;作为&#34;每月利率&#34;转换为小数百分比,指数应保持为整数,只有否定。
让您的代码保持原样:
p = int(input('Enter the amount of the loan: '))
r = float(input('Enter the interest rate: '))
d = int(input('Enter the length of loan in months: '))
mr = (r/100)/12 # convert annual interest rate to a decimal monthly interest rate
if p > 0 and r > 0 and d > 0:
m_payment = (p*mr)/(1-((1+mr) ** -d))
t_interest = d*m_payment-p
else:
print('Enter a postive number')
print('Monthly payment: ${0:.2f}'.format( m_payment))
print('Total interest paid: ${0:,.2f}'.format(round(t_interest))) # given answer appears rounded
演示:
Enter the amount of the loan: 18000
Enter the interest rate: 5.25
Enter the length of loan in months: 60
Monthly payment: $341.75
Total interest paid: $2,505.00