0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

借金額返済期間プログラム

Posted at

Python学習一環で、借金を一定額払い続けた場合、どのくらいの返済期間がかかり、借金がなくなるまでの金額を調べるプログラムを作ってみました。

環境:
Google colaboratory
目的:
借金額から返済までどのくらいの期間がかかり、どのような返済額になっていくのか表示する。

フロー:
① 借金額記載
② 利息年率(%)
③ 返済額/月
④ 借金額と返済額から返済額より借金額が少なくなるまで、月数と金額表示
⑤ 返済額より借金額が少なくなった後の月数と返済総額表示

借金の金額と、利息の年利率(%)、月々の返済額を入力すると、毎月、借金がなくなるまで月数と借金の金額を表示し、月々の借金は、借金の利息年利率/12(月割り)分増加するが、返済分だけ減るというイメージのプログラムを作成します。

入力欄を設け、debt, rate, payment変数を作る。今まで返済した合計のカウント並びかかった月数をカウントする為、total = 0, month = 0 と設定する。

debt = int(input('借金額はいくらですか?> '))
rate = float(input('年利率(%)表示して下さい。> '))
payment =  int(input('返済額の設定して下さい。> '))
total = 0
month = 0

次にdebtの返済額がどのくらいまで繰り返されるかをWhile文を使って、paymentを下回るまでとします。月数は1ヶ月ずつ増やし、残りの返済額には、debtに利率の12ヶ月の%表記(100)を掛け合わせたものから毎月のpaymentを差し引くようにします。


while debt > payment :
    month += 1
    debt = debt*(1 + rate/12/100) - payment
    print(str(month)+'月: 返済額',payment,'','残り',\
    int(debt),sep=' ')
    total += payment

更にdebtがpaymentを下回った後の返済額と返済を表示します。ここでのdebt変数は、上記debtを基にしたdebtになります。int(total)のtotalは、total +で今までpaymentでの合計値とdebtからの足算になります。

debt = debt*(1 + rate/12/100)
total += debt
print(str(month)+'月: 返済額',int(debt),'','これで完済になります。','返済総額: ',\
int(total),'',sep=' ')

まとめコード

debt = int(input('借金額はいくらですか?> '))
rate = float(input('年利率(%)表示して下さい。> '))
payment =  int(input('返済額の設定して下さい。> '))
total = 0
month = 0
while debt > payment :
    month += 1
    debt = debt*(1 + rate/12/100) - payment
    print(str(month)+'月: 返済額',payment,'','残り',\
    int(debt),sep=' ')
    total += payment
month += 1
debt = debt*(1 + rate/12/100)
total += debt
print(str(month)+'月: 返済額',int(debt),'','これで完済になります。','返済総額: ',\
int(total),'',sep=' ')

出力結果

借金額はいくらですか?> 10000
年利率(%)表示して下さい。> 10
返済額の設定して下さい。> 1000
1月: 返済額 1000 円 残り 9083
2月: 返済額 1000 円 残り 8159
3月: 返済額 1000 円 残り 7227
4月: 返済額 1000 円 残り 6287
5月: 返済額 1000 円 残り 5339
6月: 返済額 1000 円 残り 4384
7月: 返済額 1000 円 残り 3420
8月: 返済額 1000 円 残り 2449
9月: 返済額 1000 円 残り 1469
10月: 返済額 1000 円 残り 481
11月: 返済額 485 円 これで完済になります。 返済総額:  10485 円

振り返り
while debt > paymentへの理解が最初難しかった点が挙げられます。また、その処理を終え、返済総額に関わるint(total)の理解がなかなかできませんでした。今回は、借金額から利息がかかった状態での返済期間と返済額を表すものでしたが、次回は、逆に、目標設定額を決め、複利にて目標設定額を達成する期間と増加額を表すプログラムを作成したいと思います。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?