2
balance = 4213
annualInterestRate = 0.2
monthlyPaymentRate =0.04
monthInterestRate = annualInterestRate / 12
monthlyPayment = (monthlyPaymentRate*balance)
newBalance= (balance-monthlyPayment) * (1 + monthInterestRate)
month = 0
while month < 12:
    month += 1
    newBalance=(balance-monthlyPayment)*(1 + monthInterestRate)
    balance = newBalance
    monthlyPayment = (monthlyPaymentRate*newBalance)

    print ("Month: " + str(month))
    print ("Minimum monthly payment: " + str(round(monthlyPayment,2)))
    print ("Remaining balance: " + str(round(newBalance, 2)))

所以,我有上面的代码,我试图让它显示以下输出:

Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 164.48
Remaining balance: 4013.2
Month: 3
Minimum monthly payment: 160.53
Remaining balance: 3916.89
Month: 4
Minimum monthly payment: 156.68
Remaining balance: 3822.88
Month: 5
Minimum monthly payment: 152.92
Remaining balance: 3731.13
Month: 6
Minimum monthly payment: 149.25
Remaining balance: 3641.58
Month: 7
Minimum monthly payment: 145.66
Remaining balance: 3554.19
Month: 8
Minimum monthly payment: 142.17
Remaining balance: 3468.89
Month: 9
Minimum monthly payment: 138.76
Remaining balance: 3385.63
Month: 10
Minimum monthly payment: 135.43
Remaining balance: 3304.38
Month: 11
Minimum monthly payment: 132.18
Remaining balance: 3225.07
Month: 12
Minimum monthly payment: 129.0
Remaining balance: 3147.67
Total paid: 1775.55
Remaining balance: 3147.67

出于某种原因,我不断得到以下结果而不是上述结果......有人可以指出我做错了什么吗?非常感谢你!

Month: 1
Minimum monthly payment: 164.48
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 160.53
Remaining balance: 4013.2
Month: 3
Minimum monthly payment: 156.68
Remaining balance: 3916.89
Month: 4
Minimum monthly payment: 152.92
Remaining balance: 3822.88
Month: 5
Minimum monthly payment: 149.25
Remaining balance: 3731.13
Month: 6
Minimum monthly payment: 145.66
Remaining balance: 3641.58
Month: 7
Minimum monthly payment: 142.17
Remaining balance: 3554.19
Month: 8
Minimum monthly payment: 138.76
Remaining balance: 3468.89
Month: 9
Minimum monthly payment: 135.43
Remaining balance: 3385.63
Month: 10
Minimum monthly payment: 132.18
Remaining balance: 3304.38
Month: 11
Minimum monthly payment: 129.0
Remaining balance: 3225.07
Month: 12
Minimum monthly payment: 125.91
Remaining balance: 3147.67
4

4 回答 4

2

您算法中的问题是每月付款是根据新余额计算的。

关于你的 Python 代码的几点:

1-在python中编码时使用下划线而不是camelCase

2-在打印时使用字符串格式而不是 str()

3- for 是比 while here 更好的选择

4-不需要时不需要过度使用括号

balance = 4213
annual_interest_rate = 0.2
monthly_payment_rate =0.04
monthly_interest_rate = annual_interest_rate / 12
monthly_payment = monthly_payment_rate * balance
new_balance= (balance - monthly_payment) * (1 + monthly_interest_rate)

for month in range(1, 13):
    monthly_payment = monthly_payment_rate * balance
    balance = (balance - monthly_payment) * (1 + monthly_interest_rate)

    print('Month: %d \n Minimum monthly payment: %g \n Remaining balance: %g'\
          % (month, round(monthly_payment, 2), round(balance,2)))
于 2013-02-23T05:20:31.707 回答
1

考虑 while 循环中语句的顺序。

于 2013-02-23T04:43:19.810 回答
1

您的每月付款是一个月。

在您的计算中,每月付款 ( monthlyPayment) 是根据新余额 ( newBalance) 或在您付款后计算的。

于 2013-02-23T06:42:04.153 回答
-1
balance = int(raw_input("Amount of money: "))

annualRate = float(raw_input("Annual interest rate: "))

monthlyPayment = float(raw_input("Minimum monthly payment rate: "))

total = 0
for month in range(1,13):

    minMonthlyPayment = monthlyPayment * balance

    unpaid = balance - (balance*monthlyPayment)

    balance = unpaid + ((annualRate/12)*unpaid)

    total = total + minMonthlyPayment
    print "Month: " + str(month)
    print "Minimum monthly payment: " + str(round(minMonthlyPayment, 2))
    print "Remaining balance: " + str(round(balance, 2))
print "Total paid: " + str(round(total, 2))
print "Remaining balance: " + str(round(balance, 2))
于 2014-02-25T11:47:24.007 回答