May-03-2017, 10:13 AM
Hey guys I am pulling my hair out over this program.
Basically;
I am instructed to write a program that calculates the total amount of savings for 1 to 10 years with interest ranging from 2.00% to 5.50%. Also I am required to calculate the total savings made if initial investment was $100 initially using the following equation:
S = P (1 + i/n)
Where:
• S is the total savings made
• P is the initial value invested (set this to $100)
• j is the annual interest rate (set this to the interest rate / 100)
• n is the number of times that interest is compounded per year (this is monthly so set this value to 12)
• t is the number of years that money is saved for
Display the results. An example of the layout/formatting what my output SHOULD look like below:
**Savings on initial investment of $100.00**
Years: 1 2 3 4 5 6 7 8 9 10
interest rate: 2.00% 102.02 104.08 106.18 108.32 110.51 112.74 115.01 117.34 119.70 112.12
(This formula continues from 2.00% to 5:50%)
For some strange reason I seem to be off a couple decimal places with my output as I have been trying to get it to work all day. Its not in the correct format either which is something I am also stuck on
I have been given some hints too but I am a little confused as what they are portraying
Hints:
• line two of the header displaying the years can be created using a fixed string or another for-loop - whichever you prefer.
• tabs between the items of output can help in getting even spacing.
• get one for-loop (the one that creates a single row) working correctly, then wrap it in another for-loop, that creates new rows.
o e.g. you could have a variable called interest and set it to 2.00
o then use a single for-loop to create the first row of the table
o once this is going, take the for-loop statement and surround it with another for-loop that changes interest
Anyway
Here is what I have so far. Sorry if the explanation is poor the instructions were confusing enough but if someone could point me in the right direction that would be awesome and much appreciated XD
Basically;
I am instructed to write a program that calculates the total amount of savings for 1 to 10 years with interest ranging from 2.00% to 5.50%. Also I am required to calculate the total savings made if initial investment was $100 initially using the following equation:
S = P (1 + i/n)
Where:
• S is the total savings made
• P is the initial value invested (set this to $100)
• j is the annual interest rate (set this to the interest rate / 100)
• n is the number of times that interest is compounded per year (this is monthly so set this value to 12)
• t is the number of years that money is saved for
Display the results. An example of the layout/formatting what my output SHOULD look like below:
**Savings on initial investment of $100.00**
Years: 1 2 3 4 5 6 7 8 9 10
interest rate: 2.00% 102.02 104.08 106.18 108.32 110.51 112.74 115.01 117.34 119.70 112.12
(This formula continues from 2.00% to 5:50%)
For some strange reason I seem to be off a couple decimal places with my output as I have been trying to get it to work all day. Its not in the correct format either which is something I am also stuck on
I have been given some hints too but I am a little confused as what they are portraying
Hints:
• line two of the header displaying the years can be created using a fixed string or another for-loop - whichever you prefer.
• tabs between the items of output can help in getting even spacing.
• get one for-loop (the one that creates a single row) working correctly, then wrap it in another for-loop, that creates new rows.
o e.g. you could have a variable called interest and set it to 2.00
o then use a single for-loop to create the first row of the table
o once this is going, take the for-loop statement and surround it with another for-loop that changes interest
Anyway
Here is what I have so far. Sorry if the explanation is poor the instructions were confusing enough but if someone could point me in the right direction that would be awesome and much appreciated XD
import sys if sys.hexversion < 0x3000000: rng = xrange inp = raw_input else: rng = range inp = input def getter_fn(datatype): if datatype == str: return inp else: def fn(prompt=''): while True: try: return datatype(inp(prompt)) except ValueError: pass return fn get_float = getter_fn(float) get_int = getter_fn(int) def main(): principal = get_float("Initial investment amount? ") periods = get_int ("How many years") deposit = get_float("Annual deposit amount? ") apr = get_float("Annual interest rate (in percent)? ") / 100 deposits = [deposit] * periods no_deposits = [0.] * (- periods) amount = principal for yr, d in enumerate(deposits + no_deposits, 1): amount = (amount + d) * (1. + apr) print('After {:>2d} year{} you have: $ {:>10.2f}'.format(yr, 's,' if yr > 1 else ', ', amount)) if __name__ == '__main__': main()My output:
Initial investment amount? 100 How many years10 Annual deposit amount? 0 Annual interest rate (in percent)? 2.25 After 1 year, you have: $ 102.25 After 2 years, you have: $ 104.55 After 3 years, you have: $ 106.90 After 4 years, you have: $ 109.31 After 5 years, you have: $ 111.77 After 6 years, you have: $ 114.28 After 7 years, you have: $ 116.85 After 8 years, you have: $ 119.48 After 9 years, you have: $ 122.17 After 10 years, you have: $ 124.92 Process finished with exit code 0Thanks guys for any help really appreciate it in advance :)