PS4 Q1 -- code doesn't work. Also not sure how can I compile the answers into one tuple / list. Anyone care to take a look? Much appreciated!! def nestEggFixed(numSalary, numSave, numgrowthRate, numYears): Years = 0 Fund = numSalary * numSave*0.01 for Years in range (0,numYears+1): Fund = Fund + numSalary * numSave * 0.01 Years = Years +1 return [Fund] def testNestEggFixed(): salary = 10000 save = 10 growthRate = 15 years = 5 savingsRecord = nestEggFixed(salary,save,growthRate, years) print savingsRecord
This the 2008 class?
Manipulating the index variable of a for loop in the code is a very bad idea.
Yep this is the 2008 class :)
rsmith6559 thanks for the help! could you illustrate a bit more on what you said?
``` for Years in range (0,numYears+1): Fund = Fund + numSalary * numSave * 0.01 Years = Years +1 return [Fund] ``` means: Make some range that you will loop over and call it Years. Do some math to calculate Fund Increment Years (even though the loop would do this itself) Break out of the loop before completion and return Fund at where it is now.
It might be that "return [Fund]" should not be indented.
st3ph.n is right. The function shouldn't be returning each iteration. If you have something like: for i in xrange( 5 ): print i You'll get (big surprise): 0 1 2 3 4 If you start mucking with i, it'll start running into scope issues, and can really make for some interesting logical problems for you to fix.
Yes, it has multiple problems, that is why I did an explanation of what it was doing.
Join our real-time social learning platform and learn together with your friends!