-2

如果一个人每个月只支付信用卡公司要求的最低月供,请编写一个程序计算一年后的信用卡余额。

我正在尝试编写一个程序来打印一年后要支付的最终余额

balance = 4213

annualInterestRate = 0.2

monthlyPaymentRate = 0.04

month = 1

new_balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))


while month < 13:
    print "Month: " + str(month)
    print "Minimum monthly payment: " + str(round(minimum_monthly_payment, 2))
    print "Remaining balance: " + str(round(new_balance, 2))
    month += 1

我的输出显示了这一点:

Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89

Month: 2
Minimum monthly payment: 168.52
Remaining balance: 4111.89

Month: 3
Minimum monthly payment: 168.52
Remaining balance: 4111.89

但我应该得到这样的东西:

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.8
4

3 回答 3

0

你在同一行做 3 次:new_balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))

您在这里没有任何变量,这些变量将在每个 while 循环后更新。您应该更新balance以每次获得较低的数字。

于 2012-10-08T14:33:00.527 回答
0

您的值似乎没有更新,因为您忘记每月更新余额!尝试在balance每个循环中更新一次变量,如下所示:

while month < 13:
    print "Month: " + str(month)
    print "Minimum monthly payment: " + str(round(minimum_monthly_payment, 2))
    print "Remaining balance: " + str(round(balance, 2))
    balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))
    month += 1

此外,为了使您的代码更易于阅读(并且更加 Pythonic),您可以使用 Python 的内置列表结构,而不是使用自己的 while 循环:

for month in range(1, 13):
    print "Month: %d" % month
    print "Minimum monthly payment: %d" % round(minimum_monthly_payment, 2)
    print "Remaining balance: %d" % round(balance, 2)
    balance = (balance - (balance * monthlyPaymentRate)) * (1 + (annualInterestRate / 12)
于 2012-10-08T14:34:04.737 回答
0

你不需要变量new_balance,你应该只修改变量balance

while month < 13:
    print "Month: " + str(month)
    print "Minimum monthly payment: " + str(round(minimum_monthly_payment, 2))
    balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))
    print "Remaining balance: " + str(round(balance, 2))
    month += 1

输出:

Month: 1
Minimum monthly payment: 168.52
Remaining balance: 4111.89
Month: 2
Minimum monthly payment: 168.52
Remaining balance: 4013.2
Month: 3
Minimum monthly payment: 168.52
Remaining balance: 3916.89
Month: 4
Minimum monthly payment: 168.52
Remaining balance: 3822.88
Month: 5
Minimum monthly payment: 168.52
Remaining balance: 3731.13
Month: 6
Minimum monthly payment: 168.52
Remaining balance: 3641.58
Month: 7
Minimum monthly payment: 168.52
Remaining balance: 3554.19
Month: 8
Minimum monthly payment: 168.52
Remaining balance: 3468.89
Month: 9
Minimum monthly payment: 168.52
Remaining balance: 3385.63
Month: 10
Minimum monthly payment: 168.52
Remaining balance: 3304.38
Month: 11
Minimum monthly payment: 168.52
Remaining balance: 3225.07
Month: 12
Minimum monthly payment: 168.52
Remaining balance: 3147.67
于 2012-10-08T14:38:13.210 回答