-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter7.py
39 lines (33 loc) · 1.15 KB
/
chapter7.py
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
def isfloat(value):
try:
float(value)
return True
except ValueError:
return False
def prompt_for_numeric(prompt: str):
while 1:
value = input(prompt + ' --> ')
if not value or not isfloat(value):
print("I need numeric value")
else:
break
return value
def main():
dash = '-' * 40
print('Welcome to the Chapter 7 exercise: Applying fractions')
print("Let's calculate interests!!")
initial = prompt_for_numeric('Enter the initial amount to invest')
interest = prompt_for_numeric('Enter the interest earned as percentage')
years = prompt_for_numeric('Enter the number of years to calculate')
print('Calculating for ' + years + ' years ')
print(dash)
print('{:<10s}{:>4s}{:>12s}'.format('Year', 'Initial', 'Final'))
print(dash)
interest_fraction = float(interest) / 100
acumulator = float(initial)
for year in range(1, int(years) + 1):
final = acumulator + acumulator * interest_fraction
print('{:<10d}{:>4.2f}{:>12.2f}'.format(year, acumulator, final))
acumulator = final
if __name__ == "__main__":
main()