can't multiply sequence by non-int of type 'float'
Solution 1:
for i in growthRates:
fund = fund * (1 + 0.01 * growthRates) + depositPerYear
should be:
for i in growthRates:
fund = fund * (1 + 0.01 * i) + depositPerYear
You are multiplying 0.01 with the growthRates list object. Multiplying a list by an integer is valid (it's overloaded syntactic sugar that allows you to create an extended a list with copies of its element references).
Example:
>>> 2 * [1,2]
[1, 2, 1, 2]
Solution 2:
Python allows for you to multiply sequences to repeat their values. Here is a visual example:
>>> [1] * 5
[1, 1, 1, 1, 1]
But it does not allow you to do it with floating point numbers:
>>> [1] * 5.1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
Solution 3:
You're multipling your "1 + 0.01" times the growthRate list, not the item in the list you're iterating through. I've renamed i
to rate
and using that instead. See the updated code below:
def nestEgVariable(salary, save, growthRates):
SavingsRecord = []
fund = 0
depositPerYear = salary * save * 0.01
# V-- rate is a clearer name than i here, since you're iterating through the rates contained in the growthRates list
for rate in growthRates:
# V-- Use the `rate` item in the growthRate list you're iterating through rather than multiplying by the `growthRate` list itself.
fund = fund * (1 + 0.01 * rate) + depositPerYear
SavingsRecord += [fund,]
return SavingsRecord
print nestEgVariable(10000,10,[3,4,5,0,3])