Created
March 6, 2020 14:11
-
-
Save vascoferreira25/07ffa70031f071552cad99df17085c83 to your computer and use it in GitHub Desktop.
Exercise posted on reddit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
"""Solution for the exercise IS340 - Business Application Programming | |
This exercise was posted on reddit: | |
https://www.reddit.com/r/learnprogramming/comments/fdwt9a/wrote_my_first_successful_code/ | |
This exercise is to calculate the installment schedule of a car loan.""" | |
import pandas as pd | |
def get_user_input(): | |
"""Get the user input and check if it is valid. | |
How it works | |
------------ | |
Asks the user for input, if the input is invalid, it will | |
loop until a valid input is inserted. | |
It asks for `purchase_price` and `loan_term` . | |
Returns | |
------- | |
purchase_price: float | |
Price without interest of the car | |
loan_term: int | |
Loan term in months, it can be either 24 or 36.""" | |
while True: | |
purchase_price = input("Purchase Price: ") | |
try: | |
purchase_price = float(purchase_price) | |
break | |
except: | |
print("Not a valid number") | |
while True: | |
loan_term_option = input("Loan Term (2 or 3 years): ") | |
try: | |
loan_term_option = int(loan_term_option) | |
if loan_term_option == 2 or loan_term_option == 3: | |
loan_term = loan_term_option * 12 | |
break | |
else: | |
raise ValueError | |
except: | |
print("Not a valid option") | |
return purchase_price, loan_term | |
def calculate_scheduled_installments(purchase_price, loan_term, down_payment_rate, interest_rate): | |
"""Calculate the schedule table for the installments given | |
`purchase_price` value and `loan_term` months. | |
Arguments | |
--------- | |
purchase_price: float | |
The price value without interest. | |
loan_term: int | |
Number of months for the payment. | |
down_payment_rate: | |
The first payment of the loan. | |
interest_rate: | |
Loan interest rate. | |
Returns | |
------- | |
df: Dataframe | |
A Dataframe with the data.""" | |
# Save the results in an array to later | |
# convert into a dataframe | |
results = [] | |
down_payment = down_payment_rate * purchase_price | |
initial_balance = (purchase_price - down_payment) + \ | |
((purchase_price - down_payment) * interest_rate * (loan_term / 12)) | |
monthly_payment = initial_balance / loan_term | |
for month in range(0, loan_term): | |
year = month // 12 + 1 | |
starting_balance = initial_balance - (month * monthly_payment) | |
ending_balance = starting_balance - monthly_payment | |
remaining_months = loan_term - (month + 1) | |
results.append([month+1, year, starting_balance, | |
monthly_payment, ending_balance, remaining_months]) | |
# Create a dataframe with the results' data | |
column_names = [ | |
'Month', | |
'Year', | |
'Starting Balance', | |
'Installment', | |
'Ending Balance', | |
'Remaining Installments' | |
] | |
df = pd.DataFrame(data=results, columns=column_names) | |
return df | |
if __name__ == "__main__": | |
purchase_price, loan_term = get_user_input() | |
down_payment_rate = 0.1 | |
interest_rate = 0.06 | |
installment_schedule = calculate_scheduled_installments( | |
purchase_price, loan_term, down_payment_rate, interest_rate) | |
# Save data to excel file | |
installment_schedule.to_excel('./out/busines_application_homework.xlsx') | |
print(installment_schedule) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment