How many months will it take to pay off your debt? [Python]

In this article, A program that finds how many months to complete repayment from the total debt, annual interest, and monthly repayment amount I will write.

Execution environment

How to find

** 1, create a function ** First, create a function. Pass three arguments to a function called debt. The first is the amount of debt → borrowed The second is the interest rate (annual interest rate) → annual The third is the repayment amount → repayment

debt.py


def debt(borrowed, annual, repayment):

** 2, While statement that loops until paid off ** I will write a While statement that loops until finished becomes True. First, define Finished in False and then write a While statement.

debt(2).py


def debt(borrowed, annual, repayment):
    finished = False

    while finished ==  False:

** 3, Whether the balance is greater than the repayment amount (conditional branching) ** Conditional branching is performed when the balance (borrowed) is greater than or less than the repayment amount (repayment). I will also write a code that counts how many months it took.

bedt(3).py


def debt(borrowed, annual, repayment):
    month = 0
    finished = False

        if borrowed > repayment:
            month += 1
        else:
            month += 1
            finished = True

** 4, Add interest to the debt amount and subtract the repayment amount from it ** First, change the annual interest rate to monthly interest rate. How to find *   (1 + annual / 12 / 100)   * You can find it at. This is multiplied by the debt amount to get the balance, and the repayment amount is subtracted from the balance. Also, add the repayment amount to total to calculate the total repayment amount.

debt(4).py


def debt(borrowed, annual, repayment):
    month = 0
    total = 0
    finished = False

    while finished ==  False:
        if borrowed > repayment:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100) - repayment
            total += repayment
        else:
            month += 1

By the way, the method is a little different for else, so I will explain it next.

** 5, write the processing at the time of payment ** At the time of repayment, that is, when the balance is less than the repayment amount Balanced-Instead of the repayment amount, find the balance and add that number to the total repayment amount, resulting in a balance of 0. Finally, set finished to True to break out of the loop.

This is difficult to explain in words, so I think it's faster to have a look at the code.

debt(5).py


def debt(borrowed, annual, repayment):
    month = 0
    total = 0
    finished = False

    while finished ==  False:
        if borrowed > repayment:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100) - repayment
            total += repayment

        else:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100)
            total += borrowed
            finished = True

** 6, display the result ** The print () function will print the results every month. For months that are not paid off, the month, repayment amount, and balance are displayed. For the month to be paid off, the month, repayment amount, and total repayment amount are displayed. Where it becomes a decimal (float), it is displayed with int ().

debt(6).py


def debt(borrowed, annual, repayment):
    month = 0
    total = 0
    finished = False

    while finished ==  False:
        if borrowed > repayment:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100) - repayment
            total += repayment
            print(month, 'Moon:Repayment amount', repayment,'Yen', int(borrowed), "Circle", sep='')

        else:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100)
            total += borrowed
            print(month, 'Moon:Repayment amount', int(borrowed),'Yen repayment total: ', int(total), 'Circle', sep='')
            finished = True

** 7, get data and call a function ** Get the data before calling the function. Debt and repayment amounts are integers (int) Annual interest is a decimal (float)

Finally, call the debt function and pass the obtained value.

debt(7).py


def debt(borrowed, annual, repayment):
    month = 0
    total = 0
    finished = False

    while finished ==  False:
        if borrowed > repayment:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100) - repayment
            total += repayment
            print(month, 'Moon:Repayment amount', repayment,'Yen', int(borrowed), "Circle", sep='')

        else:
            month += 1
            borrowed = borrowed * (1 + annual / 12 / 100)
            total += borrowed
            print(month, 'Moon:Repayment amount', int(borrowed),'Yen repayment total: ', int(total), 'Circle', sep='')
            finished = True


b = int(input("Total debt →"))
a = float(input("Annual interest →"))
r = int(input("Monthly repayment amount →"))
debt(b, a, r)

Execution result

Total debt → 200,000
Annual interest → 12
Monthly repayment amount → 50000
----------
January:Repayment amount 50,000 yen and 152,000 yen
February:Repayment amount 50,000 yen and 103520 yen
March:Repayment amount 50,000 yen and 54555 yen
April:Repayment amount 50,000 yen and 5100 yen
May:Repayment amount 5151 yen Total repayment amount:205151 yen

The result is reflected firmly.

Summary

This time, I wrote a debt repayment program.

It may have been a difficult article to understand because it is complicated and there are many parts that are difficult to explain in words. Please refer to it.

Thank you very much.

Recommended Posts

How many months will it take to pay off your debt? [Python]
[Learning Python] How many seconds does it take to brute force a 4-digit passcode randint / for / time /
How to install Python
How to install python
How to convert Youtube to mp3 and download it super-safely [Python]
Overview of Python virtual environment and how to create it
How to take a screenshot of the Chrome screen (prevent it from cutting off in the middle)
[2020.8 latest] How to install Python
How to install Python [Windows]
python3: How to use bottle (2)
[Python] How to use list 1
How to update Python Tkinter to 8.6
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
How to run Notepad ++ Python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to keep conda off
How to use Python bytes
How to install OpenCV on Cloud9 and run it in Python