如果一个人每月只支付信用卡公司要求的最低月付款,请写一个程序来计算一年后的信用卡余额。
我正在尝试编写一个程序,用于打印一年后支付的最终余额
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
答案 0 :(得分:0)
你做了3次同一行:new_balance = (balance - (balance*monthlyPaymentRate))*(1+(annualInterestRate/12))
这里没有任何变量,每个while循环后都会更新。
您应该更新balance
以获得每次较低的数字。
答案 1 :(得分: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)
答案 2 :(得分: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