Skip to content

Commit 7102a81

Browse files
committed
Create calc_compound_interest.py
Added a simple script to calculate compound interest
1 parent 173a4b1 commit 7102a81

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

calc_compound_interest.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
import getopt
5+
6+
7+
8+
# Source resources:
9+
# https://www.statology.org/compound-interest-in-python/
10+
# https://www.enki.com/post/how-to-calculate-compound-interest-in-python
11+
def calculate_compound_interest(principal: float, interest_rate: float, times_compounded: int, years_duration: int) -> float:
12+
"""
13+
INPUTS:
14+
principal = the initial deposit or loan amount
15+
interest_rate = annual interest rate in decimal form
16+
times_compounded = number of compounding periods per year
17+
years_duration = number of years the money is invested or borrowed
18+
CALCULATION:
19+
A = principal(1 + interest_rate/times_compounded)^times_compounded*years_duration
20+
RETURNS:
21+
amount = future value of the investment or loan, including interest
22+
"""
23+
amount = principal * (1 + interest_rate/times_compounded)**(times_compounded*years_duration)
24+
# or use python's pow() function:
25+
# amount = principal*(pow((1+interest_rate/times_compounded), times_compounded*years_duration))
26+
return amount
27+
28+
29+
def main(argv):
30+
# From: https://github.com/mccright/PythonStuff/blob/main/githubfilestree.py
31+
if sys.version_info < (3, 10):
32+
raise Exception("Use only with Python 3.10 or higher")
33+
34+
principal: float = ''
35+
interest_rate: float = ''
36+
times_compounded: int = ''
37+
years_duration: int = ''
38+
39+
40+
if not (sys.argv[1:]):
41+
print('Calculate future value of an investment or loan, including interest.')
42+
print('Inputs required: -p <principal> -r <interest_rate> -t <times_compounded> -y <years_duration>')
43+
"""
44+
principal = the initial deposit or loan amount
45+
interest_rate = annual interest rate in decimal form
46+
times_compounded = number of compounding periods per year
47+
years_duration = number of years the money is invested or borrowed')
48+
"""
49+
sys.exit(2)
50+
51+
try:
52+
opts, args = getopt.getopt(argv, 'hp:r:t:y:', ['principal=','rate=','times=','years='])
53+
except getopt.GetoptError as err:
54+
print(err)
55+
print('\nCalculate future value of an investment or loan, including interest.')
56+
print(f'{sys.argv[0] } -p <principal> -r <interest_rate> -t <times_compounded> -y <years_duration>')
57+
sys.exit(2)
58+
59+
try:
60+
for opt, arg in opts:
61+
if opt == '-h':
62+
print('\nCalculate future value of an investment or loan, including interest.')
63+
print(f'{sys.argv[0] } -p <principal> -r <interest_rate> -t <times_compounded_per_year> -y <years_duration>\n')
64+
sys.exit()
65+
elif opt in ("-p", "--principal"):
66+
principal = float(arg)
67+
elif opt in ("-r", "--rate"):
68+
interest_rate = float(arg)
69+
elif opt in ("-t", "--times"):
70+
times_compounded = int(arg)
71+
elif opt in ("-y", "--years"):
72+
years_duration = int(arg)
73+
except Exception as err:
74+
print(err)
75+
print('\nCalculate future value of an investment or loan, including interest.')
76+
print(f'{sys.argv[0] } -p <principal> -r <interest_rate> -t <times_compounded> -y <years_duration>\n')
77+
sys.exit(2)
78+
79+
80+
"""
81+
principal: float = ''
82+
interest_rate: float = ''
83+
times_compounded: int = ''
84+
years_duration: int = ''
85+
"""
86+
87+
total_amount = calculate_compound_interest(principal, interest_rate, times_compounded, years_duration)
88+
print(f"${principal:,.2f} at an interest rate of {interest_rate} compounded {times_compounded} times per year for {years_duration} years = ${total_amount:,.2f}")
89+
interest_earned = total_amount - principal
90+
print(f"${total_amount:,.2f} - ${principal:,.2f} = ${interest_earned:,.2f} interest.\n")
91+
92+
if __name__ == '__main__':
93+
main(sys.argv[1:])
94+
95+
96+
"""
97+
Think about using argparse instead of getopts:
98+
For example:
99+
https://github.com/mccright/PythonStuff/blob/main/lottery_numbers/PowerBall-MegaMillions.py
100+
101+
python get user input from command line
102+
https://duckduckgo.com/?ia=web&origin=funnel_home_website&t=h_&q=python+get+user+input+from+command+line&chip-select=search
103+
104+
"""

0 commit comments

Comments
 (0)