can't multiply sequence by non-int of type 'float'

后端 未结 6 1195
傲寒
傲寒 2020-12-15 03:22

level: beginner

why do i get error \"can\'t multiply sequence by non-int of type \'float\'\"?

def nestEgVariable(salary, save, growthRates):
    Sav         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-15 03:40

    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])
    

提交回复
热议问题