57

Why do I get an error of "can't multiply sequence by non-int of type 'float'"? from the following code:

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])
Joshua
  • 3,055
  • 3
  • 22
  • 37
  • I would prefer `SavingsRecord.append(fund)` instead of your `SavingsRecord += [fund,]`, it *may* be faster. – Nick T Aug 31 '10 at 20:07
  • Possible duplicate of [Why do I get TypeError: can't multiply sequence by non-int of type 'float'?](https://stackoverflow.com/questions/485789/why-do-i-get-typeerror-cant-multiply-sequence-by-non-int-of-type-float) – Stephan Weinhold Feb 09 '18 at 10:05

6 Answers6

26
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]
Jeremy Brown
  • 17,880
  • 4
  • 35
  • 28
21

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'
jathanism
  • 33,067
  • 9
  • 68
  • 86
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])
Sam Dolan
  • 31,966
  • 10
  • 88
  • 84
2

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.

nmichaels
  • 49,466
  • 12
  • 107
  • 135
  • @All: Thanks for your prompt responses. @Nathon: i'm glad you made the comment about the variable name. It's important to get syntax discipline from the start when learning programming. –  Aug 31 '10 at 20:00
1

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.

Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
0

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?