Python 不能将序列乘以“float”类型的非整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3612378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:57:00  来源:igfitidea点击:

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

pythonfloating-pointsequence

提问by

level: beginner

水平:初学者

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

为什么我收到错误“不能将序列乘以'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])

thanks Baba

谢谢爸爸

采纳答案by Jeremy Brown

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

您将 0.01 乘以增长率列表对象。将列表乘以整数是有效的(它是重载的语法糖,允许您创建带有其元素引用副本的扩展列表)。

Example:

例子:

>>> 2 * [1,2]
[1, 2, 1, 2]

回答by Sam Dolan

You're multipling your "1 + 0.01" times the growthRate list, not the item in the list you're iterating through. I've renamed ito rateand using that instead. See the updated code below:

您将“1 + 0.01”乘以growthRate 列表,而不是您正在迭代的列表中的项目。我已经改名irate并使用它。请参阅下面的更新代码:

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

回答by Sam Dolan

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?

因为growthRates 是一个序列(您甚至在迭代它!)然后将其乘以(1 + 0.01),这显然是一个浮点数(1.01)。我猜你的意思是for growthRate in growthRates: ... * growthrate

回答by Daniel Stutzbach

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.

当您尝试将浮点数乘以增长率(这是一个列表)时,您会收到该错误。

回答by jathanism

Python allows for you to multiply sequences to repeat their values. Here is a visual example:

Python 允许您将序列相乘以重复它们的值。这是一个视觉示例:

>>> [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'

回答by nmichaels

In this line:

在这一行:

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

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.

增长率是一个序列 ( [3,4,5,0,3])。您不能将该序列乘以浮点数 (0.1)。看起来你想要放在那里的东西是i

Incidentally, iis not a great name for that variable. Consider something more descriptive, like growthRateor rate.

顺便说一下,i对于那个变量来说,并不是一个好名字。考虑更具描述性的内容,例如growthRaterate