## Aslander 4 years ago PS4.3: Ok I have this odd problem...the post retirement fund...my first three amounts add up, but then amount 4, 5 do not match. I cannot find the problem with my algorithm...any thoughts?

1. Aslander

My code is this: def postretirement(savings, growthrates,expenses): savingsrecord = [] for i in range(len(growthrates)): fund = savings + float(savings*growthrates[i]) - expenses savingsrecord.append(fund) savings = fund print "SavingsRecord", savingsrecord

2. Aslander

The amounts that were specified are this: [80000.000000000015, 54000.000000000015, 24000.000000000015, -4799.9999999999854, -34847.999999999985] My last two records are slightly different.

3. Aslander

And are returned as this: SavingsRecord [80000.0, 54000.0, 24000.0, -4800.0, -39600.0]

4. Aslander

Ok I reworked my code a bit, but I am still off... Results: SavingsRecord [80000.0, 54000.0, 24000.0, -4800.0, -34800.0] def postretirement(savings, growthrates,expenses): savingsrecord = [] for i in range(len(growthrates)): if savings > 0: fund = savings + float(savings*growthrates[i]) - expenses savingsrecord.append(fund) savings = fund else: fund = float(savings*growthrates[i]) - expenses savingsrecord.append(fund) print "SavingsRecord", savingsrecord Ideas?

5. Keen

"savings*growthrates[i]" This should be savings*0.01*growthrates[i] to convert the growthrates to percentages. I too am having issues on this problem. I'll report what I find. My current output is: [80000.00000000001, 54000.000000000015, 20000.000000000015, -1964.9999999999818, -38563.70919999998]

6. Keen

This is my current code. Still baffled. #Initialize result with (savings * (1 + .01 * growthRates[0])) in each index, with #result's length being equal to the number of years of growth rates. result = [savings * (1 + .01 * growthRates[0]) - expenses] * len(growthRates) for j in range(1, len(growthRates)+1): for i in range(j, len(growthRates)): result[i] = result[i] * (1 + .01 * growthRates[i]) - expenses return result

7. joelrob

i believe you are missing compounding.try f=[] f.append(savings) for i in range(len(growthRates)): f.append (f[i]*(1+.01*growthRates[i])-expenses) del f[0] return f

8. Keen

I'm using the same code I used for the earlier steps in the assignment, so I shouldn't have compounding issues. I would have gotten the wrong answers earlier in the problem set. I think we're having issues with error propagation. I added some prints to my code to track values of variables: result[i] * (1 + .01 * growthRates[i]) - expenses = 20000.0 result[i] = 50000.0 growthrates[i] = 0 But if I type the values of those variables right into the interpreter: >>> 50000*(1+.01*1) - 30000 20500.0

9. Keen

Err, nevermind. I dunno how I missed that 0 doesn't equal 1. Derp derp.

10. Keen

Yeah, now I've got it working. My previous method was failing to properly multiply against previous iterations. I like your use of append to iterate, very slick.

11. Keen

Isn't this code: f=[] f.append(savings) the same as: f = [savings]

12. Aslander

I did not add the "1+...." into my algorithm...I will recode that part and see what I get...

13. Aslander

Hey - I changed my code and got it to work perfectly - thanks for all your help!

14. Aslander

If you want to take a look at the new trial and compare the results: def postretirement(savings, growthrates,expenses): """Growthrates as a list of percentages ex. (0.01 for 10%)""" savingsrecord = [] for i in range(len(growthrates)): if savings > 0: #fund = savings + float(savings*growthrates[i]) - expenses fund = float(savings*(1+(0.01*growthrates[i]))) - expenses savingsrecord.append(fund) savings = fund print "Expenses", expenses else: fund = float(savings*(1+0.01*growthrates[i])) - expenses savingsrecord.append(fund) savings = fund print "SavingsRecord", savingsrecord return savingsrecord

15. Aslander

The commented out code was the original equation I used...don't know why I missed the 1+, but now its there on the line just below. Thanks again.

16. Aslander

Of course, this: """Growthrates as a list of percentages ex. (0.01 for 10%)""" would no longer apply either, as I changed the code to accept just the raw percentage, rather than floating points.

17. Aslander

Isn't this code: f=[] f.append(savings) the same as: f = [savings] You are making a list of a list. So, if you take as an example, savings = [1,2,3,4,5] then f =[savings] would return [[1,2,3,4,5]]. I don't know if that would matter, unless you append savings or f with another list, because then you would have to be careful about indexes. Example: Append savings with 7, then f with 8. Notice the difference? savings = [1,2,3,4,5,6,7] f = [[1,2,3,4,5,6,7],8] So, savings[1] which is not the same as f[1].

18. Aslander

Savings[1] = 2 f[1] = 8

19. Keen

> You are making a list of a list. So, if you take as an example, > savings = [1,2,3,4,5] Except savings is just an int for that problem, not a list.

20. Aslander

If you simply need an int, then the original question: is f.append(savings) the same as f = [savings], is not really relevant, since both involve lists. If you just need an int, then use variables without list functions.