""" Author : flo date : 2017.02.16 Purpose : example of finding out the amount of principle paid to mortgage lender per year. : This version is special. Comments: Simulate the number of mortgage payments based on the loan amount : one can alter the number of months in the outer loop to reflect the : term for your mortgage. : run the example command to first see how it needs to run. : example : python mortgage.py -h """ import getopt import sys def computeMortgage(tot, mon, rate, intr, pal): for i in range(1, mon+1): # assuming 30 days per-month for x in range(1, 31): # .0450 is the assume interest amount tsum = tot * (rate/100.00) * (1.00/365.00) tot = tsum + tot print "Month\t" + str(i) + " Day\t" + str(x) + "\t" + str(round(tot, 2)) + "\t" + str(round(tsum, 2)) tot = tot - (pal + intr) def version(): print "version 0.0.3" def usage(): usage = """ usage: python mortgage.py [-l FLOAT_VALUE] [-t Integer_VALUE] [-r FLOAT_VALUE] [-i FLOAT_VALUE] [-p FLOAT_VALUE] [-h, --help] [-v, --version] usage: python mortgage.py usage: python mortgage.py -l 250000.99 -t 24 -r 4.95 -i 515.69 -p 600 usage: python mortgage.py -v usage: python mortgage.py -h options : -l, --loanamt take a loan Value amount -t, --loanterm take an actual or a hypothetical value term -r, --loanrate take the monthly interests value rate -i, --loanint take the monthly interests value amount -p, --loanprin take the monthly principle value amount -v, --version show program's version number and exit -h, --help show this help message and exit """ print usage def main(): # default values lamt, lterm, lrate, lint, lprin = 200000.99, 24, 3.89, 450, 600.99 # try the getopt function for kicks and giggles try: opts, args = getopt.getopt(sys.argv[1:], 'l:t:r:i:p:v:h', ['loanamt', 'loanterm', 'loanrate', 'loanint', 'loanprin', 'version', 'help']) except getopt.GetoptError as err: # print something and exit print str(err) usage() sys.exit(2) # pull data from getopt for o, a in opts: if o in ('-l', '--loanamt'): lamt = float(a) elif o in ('-t', '--loanterm'): lterm = int(a) elif o in ('-r', '--loanrate'): lrate = float(a) elif o in ('-i', '--loanint'): lint = float(a) elif o in ('-p', '--loanprin'): lprin = float(a) elif o in ('-v', '--version'): version() sys.exit() elif o in ('-h', '--help'): usage() sys.exit() else: assert False, "unhandled option" # compute and forecast it all computeMortgage(lamt, lterm, lrate, lint, lprin) # calling main if __name__ == "__main__": main()