1

我的代码给出了正确的结果,除了 balance=3926。最低付款:370,而应该是 360。程序应打印给定年利率的最低每月付款。给定初始余额,代码应在年底计算余额。我们正在尝试每月支付 10 美元的初始余额。如果年底还有余额,我们编写代码将余额重置为初始余额,将付款增加 10 美元,然后重试(使用相同的代码!)计算年末余额年,看看这个新的支付价值是否足够大

annualInterestRate = 0.2

balance = 3926

monthlyinterestrate = annualInterestRate/12.0

remainingBalance = balance

month = 1
total = 0

payment = 10

def CheckMinimumPayment(payment,balance):

    "Checking if payment is in correct balance"
    while(payment*12 < balance):

        payment += 10
    return payment

payment = CheckMinimumPayment(payment,balance)

while(month <= 12):
    remainingBalance = remainingBalance - payment + (annualInterestRate / 12.0) * (remainingBalance - payment)
    month += 1
    total += payment


payment = CheckMinimumPayment(payment,total+remainingBalance)

print("Lowest Payment: " + str(payment))
4

1 回答 1

0

while(month <= 12)问题在于,每次尝试新付款时,您都不会重新遍历利息循环(您所拥有的)。将该循环写入一个函数,并在每次尝试新付款时调用它。欠款总额取决于付款,因为每月支付的金额越大,意味着每月增加的利息就越少。这是我使用的:

annualInterestRate = 0.2
init_balance = 3926

monthlyInterestRate = annualInterestRate/12.0
init_payment = 10

def owedBalance(payment,balance):
    """ Calculate total owed balance after one year
        given an initial balance and montly payment"""

    for month in range(12):
        balance = (balance - payment) * (monthlyInterestRate + 1)

    return payment*12 + balance

def CheckMinimumPayment(payment,balance):
    "Checking if payment is in correct balance"

    while (payment*12 < owedBalance(payment, balance)):
        payment += 10

    return payment

min_payment = CheckMinimumPayment(init_payment,init_balance)

print("Lowest Payment: {}".format(min_payment))
于 2013-02-25T16:55:53.640 回答