-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimevalue.py
More file actions
158 lines (137 loc) · 5.66 KB
/
timevalue.py
File metadata and controls
158 lines (137 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
def presentValue1(fv, rate, years):
while True:
try:
pvFactor = (1 + rate) ** years
pv = fv/pvFactor
return pv
except ValueError:
print("Please use valid numbers.")
def futureValue1(pv, rate, years):
while True:
try:
fvFactor = (1 + rate) ** years
fv = pv * fvFactor
return fv
except ValueError:
print("Please use valid numbers.")
def presentValueOA(payment, rate, years):
while True:
try:
pvFactor = (1 - (1 + rate) ** (years * -1)) / rate
pv = payment * pvFactor
return pv
except ValueError:
print("Please use valid numbers.")
def futureValueOA(payment, rate, years):
while True:
try:
fvFactor = (((1 + rate) ** years) - 1) / rate
fv = payment * fvFactor
return fv
except ValueError:
print("Please use valid numbers.")
def presentValueAD(payment, rate, years):
while True:
try:
pvFactor = ((1 - (1 + rate) ** (years * -1)) / rate) * (1 + rate)
pv = payment * pvFactor
return pv
except ValueError:
print("Please use valid numbers.")
def futureValueAD(payment, rate, years):
while True:
try:
fvFactor = ((((1 + rate) ** years) - 1) / rate) * (1 + rate)
fv = payment * fvFactor
return fv
except ValueError:
print("Please use valid numbers.")
def getAmount(prompt):
while True:
try:
amount = float(input(prompt).strip())
if amount > 0:
return amount
else:
print("Amount must be positive.")
except ValueError:
print("Please enter a number.")
def getYears(prompt):
while True:
try:
amount = int(input(prompt).strip())
if amount > 0:
return amount
else:
print("Amount must be positive.")
except ValueError:
print("Please enter a number.")
def getRate(prompt):
while True:
try:
raw = input(prompt).strip()
if raw.endswith('%'):
raw = raw.rstrip('%')
apr = float(raw) * 0.01
else:
apr = float(raw)
if apr < 0:
print("Rate cannot be negative.")
else:
return apr
except ValueError:
print("Please enter a number.")
def askContinue(prompt):
while True:
selector = input(prompt).strip()
if selector.upper() == "Y":
break
elif selector.upper() == "N":
running = False
else:
print("Please enter Y or N.")
running = True
while running == True:
keycode = input("Welcome to the Time Value Calculator!\nEnter 1 to find present value of 1\nEnter 2 to find future value of 1\nEnter 3 to find present value of ordinary annuity\nEnter 4 to find future value of ordinary annuity\nEnter 5 to find present value of annuity due\nEnter 6 to find future value of annuity due ").strip()
if keycode == "1":
fv = getAmount("Type in the future value. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
pv = presentValue1(fv, apr, years)
print(f"Your present value is {pv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")
elif keycode == "2":
pv = getAmount("Type in the present value. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
fv = futureValue1(pv, apr, years)
print(f"Your future value is {fv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")
elif keycode == "3":
payment = getAmount("Type in the payment amount. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
pv = presentValueOA(payment, apr, years)
print(f"Your present value is {pv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")
elif keycode == "4":
payment = getAmount("Type in the payment amount. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
fv = futureValueOA(payment, apr, years)
print(f"Your future value is {fv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")
elif keycode == "5":
payment = getAmount("Type in the payment amount. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
pv = presentValueAD(payment, apr, years)
print(f"Your present value is {pv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")
elif keycode == "6":
payment = getAmount("Type in the payment amount. ")
years = getYears("Type in the number of years. ")
apr = getRate("Type in the interest rate as a percentage (ending with %) or a decimal. ")
fv = futureValueAD(payment, apr, years)
print(f"Your future value is {fv:.2f}.")
askContinue("Would you like to continue? Enter Y or N. ")