没有得到正确的输出...逻辑正确

时间:2013-02-25 16:46:01

标签: python math python-2.7 logic

除了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))

1 个答案:

答案 0 :(得分: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))