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?
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
The amounts that were specified are this: [80000.000000000015, 54000.000000000015, 24000.000000000015, -4799.9999999999854, -34847.999999999985] My last two records are slightly different.
And are returned as this: SavingsRecord [80000.0, 54000.0, 24000.0, -4800.0, -39600.0]
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?
"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]
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
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
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
Err, nevermind. I dunno how I missed that 0 doesn't equal 1. Derp derp.
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.
Isn't this code: f=[] f.append(savings) the same as: f = [savings]
I did not add the "1+...." into my algorithm...I will recode that part and see what I get...
Hey - I changed my code and got it to work perfectly - thanks for all your help!
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
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.
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.
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].
Savings[1] = 2 f[1] = 8
> 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.
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.
Join our real-time social learning platform and learn together with your friends!