2

この質問はpython 2.7用です。この質問では、クレジット カードの残高を 12 か月以内に完済するために必要な、毎月の固定の最低支払い額を計算するプログラムを作成するよう求めています。毎月支払われる金額。

この問題では、最低月額料金は扱いません。

次の変数には、以下に説明する値が含まれます。

プログラムは 1 行を出力する必要があります: 1 年以内にすべての負債を完済する最低の毎月の支払いです。

月末(当月分の支払い後)の残高に応じて、毎月複利で利息を計算するとします。毎月の支払いは $10 の倍数である必要があり、すべての月で同じです。この支払いスキームを使用すると、残高がマイナスになる可能性があることに注意してください。これは問題ありません。必要な数学の要約を以下に示します。

月々の利率 = (年利率) / 12 毎月更新される残高 = (以前の残高 - 毎月の最低支払額) x (1 + 月々の利率)

質問のコードを思いつきました。しかし、無限ループを繰り返しました。

    b = balance = 3329
    air = annualInterestRate = 0.2
    monthlyInterestRate = (air/12)
    mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)
    month = 0
    while month <= 12:
        b = ((b - mmp) * (1 + (air/12)))
        month = 1 + month
        if b <= 0 and month == 12:
           break
        elif b > 0 and month == 12:
           b = balance
           month = 0
           mmp = minimumMonthlyPayment + 10.00
    print str('Lowest Payment: ' + str(round(mmp, 2)))

誰かがこのコードを修正するのを手伝ってくれますか? 与えられた残高の場合、最低支払い額は 310 です...これを取得する方法がわかりません...

4

9 に答える 9

2
monthlyPayment = 0
monthlyInterestRate = annualInterestRate /12
newbalance = balance
month = 0

while newbalance > 0:
    monthlyPayment += 10
    newbalance = balance

    for month in range(1,13):
        newbalance -= monthlyPayment
        newbalance += monthlyInterestRate * newbalance
        month += 1
print " Lowest Payment:", monthlyPayment
于 2015-01-19T09:27:45.117 に答える
1

このコードは少し奇妙です。つまり、次のような行です。

   mmp = minimumMonthlyPayment = (balance * monthlyInterestRate)

二重の等号で。

このコードはスタックしません:

balance = 5000
annualInterestRate = 0.2
monthlyInterestRate = (annualInterestRate/12)
minimumMonthlyPayment = (balance * monthlyInterestRate)
month = 0
while month <= 12:
    balance = ((balance - minimumMonthlyPayment) * (1 + monthlyInterestRate))
     month = 1 + month
    if balance <= 0 and month == 12:
        break
    elif balance > 0 and month == 12:
        month = 0
        minimumMonthlyPayment + 1.00

print str('Lowest Payment: ' + str(round(minimumMonthlyPayment, 2)))

しかし、あなたが探している答えは返されません。

于 2012-10-16T03:32:16.427 に答える
1

これは基本的に、randomizertech の回答のわずかに改善されたバージョンであり、ユーザーが初期残高と年利の固定値に対してのみ行うのではなく、さまざまな変数を入力できるようにします。そして最後に、ユーザーに役立ついくつかの変数を出力します。

InitialBalance = float(raw_input("Enter your current balance: "))
InterestRate = float(raw_input("Enter the yearly interest rate as a decimal: "))


monthlyPayment = 0
monthlyInterestRate = InterestRate/12
balance = InitialBalance


while balance > 0:
    monthlyPayment += 10
    balance = InitialBalance
    numMonths = 0

    while numMonths < 12 and balance > 0:

        numMonths += 1

        interest = monthlyInterestRate * balance

        balance -= monthlyPayment

        balance += interest

    balance = round(balance,2)

print "RESULT"
print "Monthly payment to pay off debt in 1 year: " , monthlyPayment
print "Number of months need to pay the debt: " , numMonths
print "Balance after debt is paid: " , balance
于 2015-12-12T20:02:00.223 に答える
1

これにより、すべてのケースで正しい答えが得られるはずです。

monthlyPayment = 10
monthlyInterestRate = annualInterestRate /12
newbalance = balance - 10

while newbalance > 0:
    monthlyPayment += 10
    newbalance = balance
    month = 0

    while month < 12 and newbalance > 0:
        newbalance -= monthlyPayment
        interest = monthlyInterestRate * newbalance
        newbalance += interest
        month += 1
    newbalance = round(newbalance,2)
print " Lowest Payment:", monthlyPayment
于 2014-09-08T18:33:11.380 に答える
1

これはかなり簡単な方法だと思います:

x = 50
o = balance
a = [1,2,3,4,5,6,7,8,9,10,11,12]
monthly = annualInterestRate/12

while balance>0:
    for months in a:
        u = balance - x
        balance = u + (monthly * u)
    if balance > 0:
        x += 10
    else:
        break
print x
于 2015-01-21T23:02:58.010 に答える
1
balance = 3329
annualInterestRate = 0.2
monthlyInterestRate = annualInterestRate/12
monthlyPayment = 0
newbalance = balance
while newbalance > 0:
monthlyPayment += 10
newbalance = balance
month = 1
while month <= 12 and newbalance > 0:
    newbalance -= monthlyPayment
    newbalance += (monthlyInterestRate * newbalance)
    month += 1    
print "Lowest Payment:",monthlyPayment

これがあなたの疑問に対する最良の解決策になると思います..そして答えを満たします

于 2014-09-10T11:55:48.887 に答える