""" Author : flo date : 2017.02.19 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. : Issues : getopt lib wont seem to take long version , hummm...... ideas ?? : still cant quite grasp the getopt function, since i wanted to use both : "--help, --version, --loanamt, --loanterm, --loanrate, --loanint, --loanprin" example : python mortgage-0.0.4.py -h example : python mortgage-0.0.4.py -v updated : fixed isdigit() and forgot an else statement """ 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.4" def usage(): usage = """ usage: python mortgage.py [-l FLOAT_VALUE] [-t Integer_VALUE] [-r FLOAT_VALUE] [-i FLOAT_VALUE] [-p FLOAT_VALUE] [-h] [-v] 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, take a loan Value amount -t, take an actual or a hypothetical value term -r, take the monthly interests value rate -i, take the monthly interests value amount -p, take the monthly principle value amount -v, show program's version number and exit -h, show this help message and exit """ print usage def main(): # default values lamt, lterm, lrate, lint, lprin = 200000.99, 10, 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:ver:hlp', ['loanamt', 'loanterm', 'loanrate', 'loanint', 'loanprin']) 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'): if a.replace('.','',1).isdigit(): lamt = float(a) else: print "Please enter your loan amount in digit form. " sys.exit(2) elif o in ('-t', '--loanterm'): if a.isdigit(): lterm = int(a) else: print "Please enter your loan duration in digit form. " sys.exit(2) elif o in ('-r', '--loanrate'): if a.replace('.','',1).isdigit(): lrate = float(a) else: print "Please enter your loan rate in digit form. " sys.exit(2) elif o in ('-i', '--loanint'): if a.replace('.','',1).isdigit(): lint = float(a) else: print "Please enter your loan accrual monthly interests in digit form. " sys.exit(2) elif o in ('-p', '--loanprin'): if a.replace('.','',1).isdigit(): lprin = float(a) else: print "Please enter your loan accrual monthly principle in digit form. " sys.exit(2) 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()