I am creating a script that allows me to calculate the future value of an investment at the end of x years, but I am having a little trouble writing the summation syntax in my script. I am not familar with the sum syntax so I do not know how it works. Any suggestions on how I should type it out? As you can see, I am new to Python.

``````def main():
print "This program calculates the future value"
print "of a 10-year investment."
principal = input("Enter the amount invested each year: ")
interest_rate = input("Enter the annual interest rate: ")
year = input("Enter the number of years: ")
for i in range(1,year+1):
value = sum(principal * (1 + interest_rate)**i)  #error here =(
print "The value in" , i , "years is" , value

main()``````
``sum(sequence)``

you are not giving it a sequence

I agree with Sillyboy, sum is not a syntax, it's a function which applies to a sequence. Here I think that the expression that you put in the sum is the value of your investment, so you can simply drop the sum. However I think your program is confusing because you say that the principal is invested each year.

``````for i in range(year):
principal  += principal * interest_rate``````

- Have fun!

I am trying to make my program to display the accumulative value for each year and not just displaying the future value for that year alone. For example, I am trying to turn the summation formula into my loop in Python
n
E principal * (1+interest_rate)^i
i=1
The E is the summation sign. And for the confusion in my program, my instructor wanted the program to look exactly like that. Please advise.

Is this a programming course or a business course? If it is a programming course, I think "exactly like that" is taking him too literally, as learning looping/control is much better than trying to mimick sums (in less efficient ways). If it is a business course (or something other than programming), you need to refine your code to use a sequence, you need to take elements up to "i" not just "i". good luck

It is a programming class =). And well, on the handout that he gave us. It said "you programs must match mine exact." So I am doing that to be on the safe side heh.

I highly doubt he wants your code to match his, the chance of code being identical is usually rare and should raise concerns of plagarism. Anyway, have you got any further?

I don't think he wants my code to match his either, just the output has to be the same. And unfortunately, I am still stuck =(. I have spent over 2 hours trying to figure it out.
Let say when I run the program, I want to use the initial principal be 50 and the interest rate be .05 in 3 years.
I want the program to say that after the first year the value would be(50*1.05=52.5) and the second year would be (50*1.05^2+52.5=107.625) and the third year would be (50*1.05^3+107.625=165.50625)

Any advise on how to make the program do that kind of loop would be helpful.

It is a programming class =)"you programs must match mine exact."

Mhh, I guess this teacher isn't teaching you programming, but conforming!! Programming is all about creativity and not copy and paste. Why does he want you to do that?

There must be some misunderstanding here. I am not copy and pasting, he showed us an output and tells us to write a program that give that same output. And I am guessing that he wants us to get familiar with the basics first since the class only started last week.

I want the program to say that after the first year the value would be(50*1.05=52.5) and the second year would be (50*1.05^2+52.5=107.625) and the third year would be (50*1.05^3+107.625=165.50625)

That does not appear to be correct IMO. In the second year you have the same \$50 drawing interest twice for the first year, once in the \$52.50 and once in the \$50*1.05^2. You have \$50.00 that will be paid interest every year
\$50 * 1.05 over some range of years
or you will have accumulated interest
new_principle_interest *= 1.05 (or += .05) over some range of years. First get the math down and then worry about converting it into code.

I want the program to say that after the first year the value would be(50*1.05=52.5) and the second year would be (50*1.05^2+52.5=107.625)

That does not appear to be correct IMO. In the second year you have the same \$50 drawing interest twice for the first year, once in the \$52.50 and once in the \$50*1.05^2. You have \$50.00 that will be paid interest every year
\$50 * 1.05 over some range of years
or you will have accumulated interest
new_principle_interest * 1.05 (or += .05) over some range of years.

and the third year would be (50*1.05^3+107.625=165.50625)

If \$50 is deposited every year, which I did not see in the original problem, then this is still incorrect as each \$50 would only earn interest from the time it was deposited. First get the math down and then worry about converting it into code.

``````'''
>>>
Year 0 Cash = principal = 100
Year 1 Cash = (principal) * (1.0 + rate) = 101.0
Year 2 Cash = ((principal) * (1.0 + rate)) * (1.0 + rate) = 102.01
Year 3 Cash = (((principal) * (1.0 + rate)) * (1.0 + rate)) * (1.0 + rate) = 103.0301
>>>
'''

principal = 100
rate = 0.01
years = 3

y = 0
cash = principal
cashtxt = "principal"
print "Year",y,"Cash =", cashtxt,"=", cash

for y in range(1,years+1):
cash = cash * (1.0 + rate)
cashtxt = "(" + cashtxt + ") * (1.0 + rate)"
print "Year",y,"Cash =", cashtxt,"=", cash``````