Hey everyone,

I'm taking an intro to python class and i've run into a bit of a barrier. My assignement is listed below:

"Write a program that takes the following inputs: (i) an initial capital C, (ii) a yearly investment Y ,(iii) an interest rate (percentage) P, and (iv) a number of years N. The program should print a table of the future values for all years."

This is the actual code I have written so far:

print "This program calculates the future value of an initial capital, given a yearly investment, interest rate, and duration in years."
c = initial_capital = input ("Enter the initial capital: ")
y = yearly_investment = input ("Enter the yearly investment: ")
i = irate = input ("Enter the interest rate: ")
irate = irate/100.0
n = number_of_years = input ("Enter the number of years: ")
for i in range (n):
    (c + y) * i
    d = (c + y) + ((c + y) * i)
    print "After year", n
    print "you'll have", d

#I guess my question is how to organize the program and how to get my for loop to work properly. Thanks for the help!

-John

Editor: added code tags

Recommended Answers

All 3 Replies

Hi John,

If I understand things correctly, then first off, line 10 is wrong. Instead of saying

print "After year", n

you ought to say

print "After year", i

because the variable that represents the current year in the for-loop is i, not n, which is the total number of years.

Also, are you sure your formula for calculating interest is correct? Would it not be easier to store the value in your bank account in a variable outside the loop, then simply add to it within the loop? A basic setup would be:

account=c
for year in range(n):
     # add the yearly investment to 'account'
     # compute the interest based on what is in 'account'
     # add the computed interest to 'account'
     # do your printing

That way seems simpler. You also want to be careful with your choice of variable names - you seem to be using 'i' to mean 'the current year' in your loop, but then you use it to mean 'the interest rate' elsewhere. Don't clutter up your namespace! If you want to loop through a number of years, just call the iterator 'year' like so

for year in range(n):

This makes things clearer for people reading the program, as well.

Another thing to remember is that range(n) gives you a list starting with 0 and ending with n-1. So if you run your simulation for 30 years, it will end up printing out "After year 0" for the first year and "After year 29" for the last year. To get around this, say

range(n+1)[1:]

which would give you a list from 0 to 30, then splice out the 0.

Hope this helps!

Thank you so much for your help G-Do! Here is the finished program.


# initializes variable "c"
c = 0.00
# initializes variable "y"
y = 0.00
# program description is displayed
print "This program calculates the future value of an initial capital, given a yearly investment, interest rate, and duration in years."
# asks user for input
c = input ("Enter the initial capital: ")
# asks user for input
y = input ("Enter the yearly investment: ")
# asks user for input
i = input ("Enter the interest rate: ")
# asks user for input
n = input ("Enter the number of years: ")
# initializes variable "new_cap"
new_cap = 0.00
# initializes variable "interest"
interest = 0.00
# sets "c" equal to "new_cap"
new_cap = c
# loop which runs for a user designated period
for year in range (n+1) [1: ]:
# commpound interest formula
new_cap = new_cap + y
interest = new_cap * i/100
new_cap = new_cap + interest
# displays list of rounded values
print "After year", year, "you'll have", round (new_cap, 2), "dollars"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.