级别:初学者

为什么会出现错误“无法将序列乘以'float'类型的非整数”?

def nestEgVariable(salary, save, growthRates):
    SavingsRecord = []
    fund = 0
    depositPerYear = salary * save * 0.01
    for i in growthRates:
        fund = fund * (1 + 0.01 * growthRates) + depositPerYear
        SavingsRecord += [fund,]
    return SavingsRecord


print nestEgVariable(10000,10,[3,4,5,0,3])

谢谢
巴巴

最佳答案

for i in growthRates:
    fund = fund * (1 + 0.01 * growthRates) + depositPerYear

应该:
for i in growthRates:
    fund = fund * (1 + 0.01 * i) + depositPerYear

您将0.01乘以growthRates列表对象。将列表乘以整数是有效的(它是重载的语法糖,它使您可以使用其元素引用的副本来创建扩展列表)。

例子:
>>> 2 * [1,2]
[1, 2, 1, 2]

10-08 04:23