|
| 1 | +# |
| 2 | +# CS 177 - project1.py |
| 3 | +# Wilson Huang - 0031114130 |
| 4 | +# This is a demonstration of the basic Python file functions. |
| 5 | +# The program accepts specific values and then displays results as table |
| 6 | +# through function operations. |
| 7 | +# |
| 8 | + |
| 9 | +# initialize any necessary variables |
| 10 | + |
| 11 | +# define main function |
| 12 | +def main(): |
| 13 | + |
| 14 | + # display information about the prgram function |
| 15 | + print("The program accepts specific values and then displays results as table.") |
| 16 | + |
| 17 | + # prompt user for purchase price,down payment,APR,periods per year,term of loan,and first payment date |
| 18 | + price = eval(input("Enter total sale price of property:")) |
| 19 | + payment = eval(input("Enter cash down payment amount:")) |
| 20 | + APR = eval(input("Enter loan interest charged per year:")) |
| 21 | + numberPerYear = eval(input("Enter number of payments per year:")) |
| 22 | + term = eval(input("Enter length of loan in years:")) |
| 23 | + firstDate = str(input("Enter first payment date in MM/DD/YY:")) |
| 24 | + |
| 25 | + # calculate the loan amount and number of payments |
| 26 | + loan = price - payment |
| 27 | + numberOfPayment = term*numberPerYear |
| 28 | + paymentAmount = round((APR/numberPerYear)*loan/(1-(1+APR/numberPerYear)**(-numberOfPayment)),2) |
| 29 | + |
| 30 | + # define loanInfo function |
| 31 | + def loanInfo(loanAmount,period,paymentTimes,annualRate,beginningDate): |
| 32 | + |
| 33 | + # initialize any necessary variables and then perform calculations |
| 34 | + rate1 = annualRate/period |
| 35 | + totalPayment1 = paymentAmount*paymentTimes |
| 36 | + totalInterest1 = totalPayment1 - loanAmount |
| 37 | + |
| 38 | + # calculate endDate using split() |
| 39 | + separation = firstDate.split("/") |
| 40 | + year = int(separation[2]) + (term-1) |
| 41 | + for i in range(numberOfPayment): |
| 42 | + if (int(separation[0])+12/period*i<=12 and i%12!=0): |
| 43 | + endDate1 = "{0}/1/{1}".format(int(separation[0])+12/numberOfPayment*i,int(separation[2])+(i-1)//12) |
| 44 | + elif (i%12==0): |
| 45 | + endDate1 = "{0}/1/{1}".format(1,int(separation[2])+(i+1)//12) |
| 46 | + else: |
| 47 | + endDate1 = "{0}/1/{1}".format(int(separation[0])+12/period*i-12*(term-1),int(separation[2])+(i-1)//12) |
| 48 | + return rate1,totalPayment1,totalInterest1,endDate1 |
| 49 | + |
| 50 | + # call loanInfo function and assign variables to the results |
| 51 | + rate,totalPayment,totalInterest,endDate = loanInfo(loan,numberPerYear,numberOfPayment,APR,firstDate) |
| 52 | + |
| 53 | + # define loanOptions function |
| 54 | + def loanOption(annualRate,period,lengthOfLoan,loanAmount): |
| 55 | + |
| 56 | + # initialize any necesssary variables |
| 57 | + differentRate = "" |
| 58 | + payment = "" |
| 59 | + format1 = "" |
| 60 | + |
| 61 | + # display table headers |
| 62 | + print("\t\t\tAlternative Loan Payment Table") |
| 63 | + print("\t\t\t","="*40) |
| 64 | + print("\t\t\t\tInterest Rates") |
| 65 | + |
| 66 | + # construct loop for headlines of different rate |
| 67 | + for indexOfRate in range(-3,4): |
| 68 | + format1 = "{0:10}%".format((annualRate+0.005*indexOfRate)*100) |
| 69 | + differentRate += format1 |
| 70 | + print(differentRate) |
| 71 | + print("# Payments","="*50) |
| 72 | + |
| 73 | + # define a for loop based on the specified range for the number of loan payments |
| 74 | + for i in range(-2,3): |
| 75 | + n = round(period*lengthOfLoan + period*i/2) |
| 76 | + payment += "\n" + str(n) |
| 77 | + |
| 78 | + # define a for loop based on the specified range of interest rates |
| 79 | + for j in range(-3,4): |
| 80 | + r = (annualRate + 0.005*j)/period |
| 81 | + amount = "$"+str(round((r*loanAmount)/(1-(1+r)**(-n)),2)) |
| 82 | + finalAmount = "{0:>11}".format(amount) |
| 83 | + payment += finalAmount |
| 84 | + print(payment) |
| 85 | + |
| 86 | + # call loanOption function |
| 87 | + loanOption(APR,numberPerYear,term,loan) |
| 88 | + |
| 89 | + # define amortize function |
| 90 | + def amortize(beginningDate,loanAmount,paymentPerMonth,annualRate,period): |
| 91 | + |
| 92 | + # initialize any necessary variables |
| 93 | + startingBalance = loanAmount |
| 94 | + separation = beginningDate.split("/") |
| 95 | + format2 = "" |
| 96 | + |
| 97 | + # display table headers |
| 98 | + print("\t\t\tLoan Amortization Table\n","="*70, |
| 99 | + "\n\tpayment\tstarting paymentinterest principle ending", |
| 100 | + "\n#\tdate\tbalance\tamount\tpaid\tpaid\tbalance", |
| 101 | + "\n====\t=====\t=====\t=====\t=====\t=====\t=====") |
| 102 | + |
| 103 | + # define a for loop based on the number of payments in the loan |
| 104 | + for i in range(numberOfPayment): |
| 105 | + number = str(1 + i) |
| 106 | + if (int(separation[0])+12/period*i<=12 and i%12!=0): |
| 107 | + paymentDate = "{0}/1/{1}".format(round(int(separation[0])+12/period*i),int(separation[2])+(i-1)//12) |
| 108 | + elif (i%12==0): |
| 109 | + paymentDate = "{0}/1/{1}".format(1,int(separation[2])+(i+1)//12) |
| 110 | + else: |
| 111 | + paymentDate = "{0}/1/{1}".format(round(int(separation[0])+12/period*i-12*(term-1)),int(separation[2])+(i-1)//12) |
| 112 | + interestPerMonth = round(startingBalance*(annualRate/period),2) |
| 113 | + principle = round(paymentPerMonth - interestPerMonth,2) |
| 114 | + endBalance = round(startingBalance - principle,2) |
| 115 | + format2 = "{0:>1}{1:>10}{2:>10}{3:>10}{4:>10}{5:>10}{6:>10}".format(number,paymentDate,"$"+str(startingBalance),"$"+str(paymentPerMonth),"$"+str(interestPerMonth),"$"+str(principle),"$"+str(endBalance)) |
| 116 | + print(format2) |
| 117 | + startingBalance = endBalance |
| 118 | + |
| 119 | + # call amortize function |
| 120 | + amortize(firstDate,loan,paymentAmount,APR,numberPerYear) |
| 121 | + |
| 122 | +main() |
0 commit comments