I am not too familiar with Python, but I am attempting to create a program which takes in a file name, worksheet name, monthly payment, user's additional monthly payment, and principal loan amount. The program should then create an excel file with the name submitted, a worksheet with the name submitted. In the excel sheet start subtracting the monthly payment from the balance write that into the excel sheet with a payment date calculated and then do the same with the additional amount and keep repeating until the balance reaches 0. I have done this using recursion, but I get the following error
zsh: segmentation fault python3 loanPayoff.py
The following is my code:
import datetime
import xlsxwriter
import sys
sys.setrecursionlimit(10**6)
# we create the workbook
def createWorkBook(workBookName, workSheetName, monthly, principal, additional, date):
#creating the workbook
workbook = xlsxwriter.Workbook(workBookName + '.xlsx')
worksheet = workbook.add_worksheet(workSheetName)
worksheet.write('A1', 'Date')
worksheet.write('B1', 'Mortgage principal amount')
worksheet.write('C1', 'Regular amount against principal')
worksheet.write('D1', 'Against principal additional')
payments(monthly, additional, principal, worksheet, 'regular', date, 1)
workbook.close()
# update the workbook's worksheet with the next date
def workBookUpdateDate(date, row, workSheet):
dateColumn = 0
workSheet.write(row, dateColumn, date)
# update the workbook's worksheet with the new balance
def workBookUpdateBalance(balance, row, workSheet):
balanceColumn = 1
workSheet.write(row, balanceColumn, balance)
# update the workbook's worksheet with the regular amount being paid
def workBookUpdateRegularAmount(regularAmount,row, workSheet):
regularAmountColumn = 2
workSheet.write(row, regularAmountColumn, regularAmount)
# update the workbook's worksheet with the additional amount being paid
def workBookUpdateAdditionalAmount(additionalAmount,row, workSheet):
additionalAmountColumn = 3
workSheet.write(row, additionalAmountColumn, additionalAmount)
# increment the date by two weeks
def addTwoWeeksToDate(date):
twoWeeks = datetime.timedelta(days=14)
newDate = date + twoWeeks
#print("DATE ......", format(newDate))
return newDate
def payments(monthly, additional, principal, workSheet, type, date, row):
while principal != 0:
if type == 'regular':
principal = principal - monthly
date = addTwoWeeksToDate(date)
workBookUpdateDate(date, row, workSheet)
workBookUpdateAdditionalAmount(0,row, workSheet)
workBookUpdateRegularAmount(monthly, row, workSheet)
workBookUpdateBalance(principal, row, workSheet)
row = row + 1
payments(0, additional, principal, workSheet, 'additional', date, row)
elif type == 'additional':
principal = principal - additional
date = addTwoWeeksToDate(date)
workBookUpdateDate(date, row, workSheet)
workBookUpdateAdditionalAmount(additional, row, workSheet)
workBookUpdateRegularAmount(0, row, workSheet)
workBookUpdateBalance(principal, row, workSheet)
row = row + 1
payments(0, additional, principal, workSheet, 'regular', date, row)
# prompt the user for the information
fileName = input('Hello what will the file be called? ')
workSheetNameToBe = input('Ok, what will the worksheet be called? ')
monthlyPaymentAmount = float(input('How much do you pay monthly? '))
additionalPaymentAmount = float(input('How much additional can you pay monthly? '))
principalAmount = float(input('How much is the principal? '))
# set the start date for today
new_date = datetime.date.today()
createWorkBook(fileName, workSheetNameToBe, monthlyPaymentAmount, principalAmount, additionalPaymentAmount, new_date)
I am currently using Python 3.8.2 on a Mac 10.15.4
Any feedback welcomed.