0

が見つかるまで次のループを続けるにはどうすればよいmmp = 310ですか? これまでのところ、私のコードで行けるのはmmp = 240. ifもう1つ声明を出すべきだと思いますか?

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)
4

1 に答える 1

0

の代わりに、その数に到達したいだけの場合にwhile Month <= 12:使用できます。while mmp < 310:またはwhile rb > 0:、すべてが支払われるまでループを続けたい場合。

数か月にわたるループが必要な場合 (ところで、質問が宿題であると言及すると、ここで一般的に高く評価されます)、数年間の外側のループを追加できます。

year = 1
while rb > 0:
    month = 1
    while month <= 12:
        # do stuff
        month = month + 1
    year = year + 1
于 2013-02-22T10:17:59.360 に答える