2012-10-08 119 views
-3

如果一個人每個月只支付信用卡公司要求的最低每月支付,請編寫一個程序計算一年後的信用卡餘額。計算信用餘額的計劃

我試圖寫一個程序,將打印期末餘額左手交一年後

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

你不需要變量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 
+0

非常感謝。我剛剛在while循環中觀看了一段視頻,只是看到了我的錯誤。我沒有把餘額作爲變量放入循環中,並且在循環之後沒有修改......謝謝。 :) – Contempt

+0

@ user1692479很高興幫助。 :)如果回答幫助你,那麼你可以通過檢查答案左邊的勾號來接受它。 –

0

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

這裏沒有任何變量,它會在每個while循環後更新。 您應該更新balance以獲得每次較低的號碼。

+0

重複的輸出是從while循環 - 絕對不是由該線,其中僅發生一次打印出來。 –

+0

好的,但你必須更新「餘額」,以保持較低的價值。否則你正在做這樣的數學:A = 1,B = 1, < - 這個失蹤 – Rob

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 

此外,爲使您的代碼更易於閱讀(以及更多Python的),你可以使用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)