dekyos

Investment Calculator

May 15th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.04 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon May 15 09:13:08 2017
  4. Investment Growth Calculator
  5. @author: MadMerlyn
  6. """
  7.  
  8. def Invest(principle, reg_payments, rate, **period):
  9.     """Principle, + regular (monthly) payments, and rate in APR (eg. 0.08)
  10.    for period use either 'years=XX' or 'months=XX'
  11.    --Prints estimated growth schedule based on anticipated rate of return
  12.    --Always prints schedule in years regardless of period selection"""
  13.     p = principle #clone value for growth calculation
  14.     r = rate/12   #reduce APR to MPR
  15.  
  16.     if 'years' in period:
  17.         period = (period['years']*12)+1
  18.     elif 'months' in period:
  19.         period = period['months']+1
  20.     else:
  21.         raise 'You did not provide an appropriate period range!'
  22.  
  23.     results = []
  24.     for t in range(period):
  25.         balance = p*(1-r**(t+1))/(1-r)      #expression for generating interest
  26.         if t != 0:
  27.             p = balance + reg_payments          #maintain total investment cost
  28.             principle = principle + reg_payments  #update principle amount
  29.         growth = p - principle               #calculate total interest
  30.        
  31.         invest_data = { #create dict for graphing
  32.                 'period' : t,
  33.                 'total' : format(p, '.2f'),
  34.                 'invested' : format(principle, '.2f'),
  35.                 'interest' : format(growth, '.2f')
  36.                 }  
  37.         results.append(invest_data) #append dicts to results table
  38.         if t % 60 == 0 and t != 0:
  39.             print("Balance after", int(t/12), "years:", format(p, '.2f'))
  40.             print("    Total invested:", format(principle, '.2f'))
  41.             print("    Total interest:", format(growth, '.2f'))
  42.  
  43.     print("Expected annual interest on final balance:")
  44.     print("                   ", format(p*rate, '.2f'))
  45.    
  46.     return results
  47.  
  48. if __name__ == '__main__':
  49.     principle = int(input('Enter starting principle: '))
  50.     payment = int(input('Enter monthly payments: '))
  51.     rate = float(input('Enter expected APR: '))
  52.  
  53.     Invest(principle, payment, rate)
Advertisement
Add Comment
Please, Sign In to add comment