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

后端 未结 6 1190
傲寒
傲寒 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])
    
    0 讨论(0)
  • 2020-12-15 03:43

    In this line:

    fund = fund * (1 + 0.01 * growthRates) + depositPerYear
    

    I think you mean this:

    fund = fund * (1 + 0.01 * i) + depositPerYear
    

    When you try to multiply a float by growthRates (which is a list), you get that error.

    0 讨论(0)
  • 2020-12-15 03:56

    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'
    
    0 讨论(0)
  • 2020-12-15 03:59

    In this line:

    fund = fund * (1 + 0.01 * growthRates) + depositPerYear

    growthRates is a sequence ([3,4,5,0,3]). You can't multiply that sequence by a float (0.1). It looks like what you wanted to put there was i.

    Incidentally, i is not a great name for that variable. Consider something more descriptive, like growthRate or rate.

    0 讨论(0)
  • 2020-12-15 04:00

    Because growthRates is a sequence (you're even iterating it!) and you multiply it by (1 + 0.01), which is obviously a float (1.01). I guess you mean for growthRate in growthRates: ... * growthrate?

    0 讨论(0)
  • 2020-12-15 04:03
    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]
    
    0 讨论(0)
提交回复
热议问题