2013-02-22 55 views
0

如何繼續下面的循環,直到找到mmp = 310?到目前爲止,我的代碼最遠的地方是mmp = 240。你認爲我應該多一個if聲明?如何計算信用卡的固定月付款

balance = 4213 
annualInterestRate = 0.2 
mir = annualInterestRate/12 
monthlyPaymentRate = 0.04 


rb = balance 
mmp = 0 
Month = 1 
while Month <= 12: 
    print('Month:' + str(Month)) 
    mmp = mmp + 10 
    print('Minimum monthly payment:' + str(mmp)) 
    ub = rb - mmp 
    rb = round(ub + (annualInterestRate/12 * ub), 2) 
    Month = Month + 1 
    print('Remaining balance:' + str(rb)) 
if rb > 0: 
    rb = balance 
    Month = 1 
    while Month <= 12: 
     print('Month:' + str(Month)) 
     mmp = mmp + 10 
     print('Minimum monthly payment:' + str(mmp)) 
     ub = rb - mmp 
     rb = round(ub + (annualInterestRate/12 * ub), 2) 
     Month = Month + 1 
     print('Remaining balance:' + str(rb)) 

else: 
    print('Lowest Payment:' + str(mmp) 

回答

0

相反的while Month <= 12:,你可以使用while mmp < 310:,如果你只是想達到這個數字。或者while rb > 0:如果你想繼續循環,直到一切都付清。

如果循環在過去幾個月是一個要求(順便說一句,這是generally appreciated here if you mention that your question is homework),你可以添加一個外環多年:

year = 1 
while rb > 0: 
    month = 1 
    while month <= 12: 
     # do stuff 
     month = month + 1 
    year = year + 1 
+0

我必須有'一個月<= 12'在我的代碼。 – upendra 2013-02-22 15:39:42

+0

@ user1755155:查看更新 – Junuxx 2013-02-22 15:44:47

+0

是的,這是作業問題,我得到了非常嚴重的困難。還有一件事是它只能是'1年',所以基本上我必須計算在'12個月'rb <0'時mmp是多少。 – upendra 2013-02-22 15:53:14