# Control Structures (Mortgage Calculator)

Back To The Index Untitled1

Let’s write a program to calculate the monthly payment and loan balance on amortized mortgages.

$$Monthly~Payment = \frac{L \times c \times (1+c)^{n}}{(1+c)^n-1}$$ Where

• L = Total amount borrowed
• c = Monthly rate of interest
• n = Number of months
In :
###################################################
#  Filename : simple_mortgage_calculator          #
#  Purpose :  To demonstrate reading data from    #
#   screen and use it in a formula.               #
#        * How to translate algebraic             #
#    expressions into python expressions          #
#                                                 #
#  Author : Niel S.                               #
#  The English Tea Company LLC                    #
###################################################

def main():

run_flag = True

while(run_flag == True):

L = input ('Enter an amount: ')
APR = input ('Enter Annual Percentage Rate: ')
N = input ('Enter Duration in years: ')

#Convert the values into floating point numbers.
L   = float(L)
c   = float (APR)/1200.  #convert annual percentage to monthly rate.
n   = float (N)*12. #convert number of years to number of months.

#Monthly payment is calculated using a formula.
P = L*(c*(1 + c)**n)/((1 + c)**n - 1)

print ('Monthly Payment: ',round(P,0))

rflag = input('Do you wish to enter another amount (Y for Yes):').strip().upper()
if not rflag == 'Y':
run_flag = False

if __name__ == '__main__':
main()

Enter an amount: 223000
Enter Annual Percentage Rate: 3.5
Enter Duration in years: 30
Monthly Payment:  1001.0
Do you wish to enter another amount (Y for Yes):Y
Enter an amount: 223000
Enter Annual Percentage Rate: 3.5
Enter Duration in years: 15
Monthly Payment:  1594.0
Do you wish to enter another amount (Y for Yes):N

Back To The Index