Skip to content

Instantly share code, notes, and snippets.

@dawranliou
Created October 13, 2016 02:44
Show Gist options
  • Save dawranliou/b4e8f19f9f1e623fb2df017e1b90a61b to your computer and use it in GitHub Desktop.
Save dawranliou/b4e8f19f9f1e623fb2df017e1b90a61b to your computer and use it in GitHub Desktop.
Simple program to calculate monthly payment and total payment
import argparse
def total_payment(principal, monthly_rate, payment_months):
return principal * (1 + monthly_rate) ** payment_months
def monthly_payment(principal, monthly_rate, payment_months):
weighted_month = ((1 + monthly_rate) ** payment_months - 1) / monthly_rate
return total_payment(principal, monthly_rate, payment_months) / weighted_month
print('--------------------------------------------')
print(' Mortgage monthly payment calculator')
print('--------------------------------------------')
print()
parser = argparse.ArgumentParser(
description='Calculate the monthly payment of your mortgage')
parser.add_argument('term', metavar='n', type=int,
help='How many years is your mortgage term')
parser.add_argument('rate', metavar='r', type=float,
help='The yearly rate in percentage of your mortgage')
parser.add_argument('principal', metavar='P', type=float,
help='The principal of your mortgage')
args = parser.parse_args()
monthly = monthly_payment(args.principal, args.rate / 12 / 100, args.term * 12)
total = monthly * args.term * 12
print('Term: {}-year'.format(args.term))
print('Rate: {}%'.format(args.rate))
print('Principal: ${}'.format(args.principal))
print('Monthly payment: ${0:.2f}'.format( monthly))
print('Total payment: ${0:.2f}'.format(total))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment