我在运行这个程序:
balance = 320000
annualInterestRate = 0.2
monthlyPayment = 0.0
monthlyInterestRate = annualInterestRate/12
low = balance / 12.0
high = ((balance * ((1.0 + monthlyInterestRate)**12.0))/12.0)
monthlyPayment = (high+low)/2
while balance != 0:
newBalance = balance
for x in range(1, 13):
lastMonthBalance = newBalance - monthlyPayment
newBalance = lastMonthBalance + ((monthlyInterestRate) * lastMonthBalance)
if -0.002 <= newBalance <= 0.002:
balance = 0
if newBalance < 0.0:
low = 0
high = monthlyPayment
elif newBalance > 0.0:
low = monthlyPayment
monthlyPayment = (low + high) / 2
print "Lowest Payment: %.2f" % monthlyPayment最初,我试图找到使我的newBalance等于0的月付款,但是由于处理时间的限制,我选择了一种不太准确的方法。
然而,当我把界限从-0.2/0.2改为-0.002/0.002时,我的答案从14578.55变为29157.09,我不明白为什么?
任何帮助都是非常感谢的。
发布于 2015-06-23 16:13:27
因此,我不完全确定为什么,但通过删除low = 0的行,代码才能正常工作。
工作守则是:
#Write a program that calculates the minimum fixed monthly payment needed in order pay off a credit card balance within 12 months. By a fixed monthly payment, we mean a single number which does not change each month, but instead is a constant amount that will be paid each month.
balance = 999999
annualInterestRate = 0.18
monthlyPayment = 0.0
monthlyInterestRate = annualInterestRate/12
low = balance / 12.0
high = ((balance * ((1.0 + monthlyInterestRate)**12.0))/12.0)
monthlyPayment = (high+low)/2
while balance != 0:
newBalance = balance
for x in range(1, 13):
lastMonthBalance = newBalance - monthlyPayment
newBalance = lastMonthBalance + ((monthlyInterestRate) * lastMonthBalance)
if -0.05 <= newBalance <= 0.05:
balance = 0
if newBalance < 0.0:
high = monthlyPayment
elif newBalance > 0.0:
low = monthlyPayment
monthlyPayment = (low + high) / 2
print "Lowest Payment: %.2f" % monthlyPayment如果有人能向我解释为什么会发生这种情况,我将非常感激。
https://stackoverflow.com/questions/31005317
复制相似问题