I was thinking about what I should do for my first python blog, and I thought of some of the basic but cool stuff that I have done using python. I arrived at the Loan Calculator that I made in python. If you are looking for how to set up python I have a blog on that which you could refer to. Anyways, thank you for joining me in this blog and without further ado, this is the first part of my script.

#Get details of loan

money_owed = float(input(‘How much money do you owe (in dollars)?\n’))

apr = float(input(‘What is the annual percentage rate of the loan?\n’))

payment = float(input(‘How much will you pay off each month (in dollars)?\n’))

months = int(input(‘How many months do you want to see the results for?\n’))

To start I created 4 variables. First, **money_owed**, I make it a float since there could be decimals involved in the loan amount. Then I created **apr** and made that a float as well since interest rates could be anywhere including but not limited to whole numbers. Next is **payment**, also a float. This is because you don’t always (if ever) have a payment that is exactly x amount of dollars and 0 cents. Lastly is **months** which I made an int since loans aren’t dealt in terms of half months or anything other than a whole month so there is no concern for decimals here. I also made sure that the variables required an input so you could designate the loan amount, apr, etc. yourself.

monthly_rate = apr/100/12

Moving on to where variables are starting to be used to create new variables, we have **monthly_rate**. Since I am using **apr** which is a float the monthly rate is automatically designated as a float data type. To get the monthly rate I did the standard calculation of the **apr** divided by 100 (percent) divided by 12 (months).

for i in range(months):

# Calculate interest to pay

interest_paid = money_owed * monthly_rate

# Add in interest

money_owed = money_owed + interest_paid

Now we get to the **For i in range(months)**. This is what causes the result set to loop for each month allowing you to see the payments over time. For the next variable created we have **interest_paid**. This is mainly for use in the text that gets returned on each loop which I will get to later. Following up on that is the **money_owed** variable again. We re-evaluate the value of the **money_owed** variable each time the loop run based on the **interest_paid** and the ** money_owed**.

if(money_owed – payment) < 0:

print(‘The last payment is’, money_owed)

print(‘You paid off the loan in’, i+1, ‘months’)

break

Wrapping it up, we get into the printing section. The first snippet is related to the **money_owed** saying that if **money_owed **minus the **payment **is less than zero,then it will print “The last payment is” and then however much was left on the **money_owed** value. Then on the next line it prints “You paid off the loan in”, whatever the value of the **i **is (how many months it looped) plus 1 since it starts from 0, “months”. Then we have a break to say that we are done with that specific string of input.

# Make payment

money_owed = money_owed – payment

print(‘Paid’, payment, ‘of which’, interest_paid, ‘was interest.’, end = ‘ ‘)

print(‘Now I owe’, money_owed)

Lastly is what we will see on each iteration of the loop. As you can see, it will return “Paid”, however much you entered for the **payment** variable, “of which”, the calculated **interest_paid** variable, “was interest.” Then we say that we want this string to end and to start another with a space at the end with the **end** = ‘ ‘. Then we print “Now I owe”, and then how much money is calculated to be owned in the **money_owned** variable that is getting looped.

This is the script as a whole as well as the results that return.

Thank you everyone for reading my blog today. I hope you have a great rest of your day and be on the lookout for any more python blogs that I might publish here. Thanks again!

Bailey McDonald

Data Engineer, Patriot Consulting

Email: bkmcdonald@patriotconsultingcorp.com | Blogs: Patriot Consulting Blogs

LinkedIn: Personal: BaileyMcDonald | Company: Patriot Consulting