我试图完成麻省理工学院问题集2的第三个问题(该课程是6.00.1x,在Edx上)。 这是关于这个问题的一些信息(正如我所说,我正在研究第三个问题,但我会附上每个问题的图片,因为它们是相互关联的):
我写了不同的程序,但它们都不起作用。 这是第一个:
savedBalance = balance
def minimumFixedMonthlyPayment(balance,annualInterestRate, high, low):
epsilon = 0.01
minimumMonthlyPayment = (high+low)/2
monthlyInterestRate = annualInterestRate / 12
savedbalance= balance
for month in range(12):
unpaidBalance = balance - minimumMonthlyPayment
balance = unpaidBalance + (monthlyInterestRate * unpaidBalance)
if abs(savedbalance-minimumMonthlyPayment*12) < epsilon:
return print(round(minimumMonthlyPayment,2))
else:
if abs(savedBalance-minimumMonthlyPayment*12) > epsilon:
high = minimumMonthlyPayment
else:
low= minimumMonthlyPayment
return minimumFixedMonthlyPayment(savedbalance,annualInterestRate,high,low)
minimumFixedMonthlyPayment(balance,annualInterestRate,(savedBalance*(1+annualInterestRate/12)**12/12) , balance/12)
预期输出为29157.09
,但我的程序会打印2666.67
。
这是第二个,预期输出相同,但程序给我一个错误:RecursionError: maximum recursion depth exceeded in comparison
。
balance = 32000
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate/12
savedBalance = balance
low = balance/12
high = (balance*(1+monthlyInterestRate)**12)/12
minimumMonthlyPayment = 0
unpaidBalance = 0
while savedBalance-minimumMonthlyPayment*12 > 0.01:
minimumMonthlyPayment = (high+low)/2
for month in range(12):
unpaidBalance = balance - minimumMonthlyPayment
balance = unpaidBalance + (monthlyInterestRate * unpaidBalance)
if balance > 0:
low = minimumMonthlyPayment
else:
high = minimumMonthlyPayment
minimumMonthlyPayment = (high+low)/2
if abs(balance) > 0:
balance = savedBalance
print("Lowest payment is: " + str(round(minimumMonthlyPayment,2)))
预期输出相同,但程序打印2812.93
。
我哪里错了?为什么?我该如何修复该程序?
谢谢你的关注!