简体   繁体   English

不能将序列乘以'float'类型的非int

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

level: beginner 级别:初学者

why do i get error "can't multiply sequence by non-int of type 'float'"? 为什么我得到错误“不能将序列乘以'int'类型的非int”?

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 谢谢巴巴

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. 您将使用growthRates列表对象乘以0.01。 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]

Python allows for you to multiply sequences to repeat their values. Python允许您将序列相乘以重复其值。 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'

You're multipling your "1 + 0.01" times the growthRate list, not the item in the list you're iterating through. 您将growRate列表的“1 + 0.01”乘以,而不是您正在迭代的列表中的项目。 I've renamed i to rate and using that instead. 我已经改名为irate和使用它。 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])

In this line: 在这一行:

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

growthRates is a sequence ( [3,4,5,0,3] ). growthRates是一个序列( [3,4,5,0,3] )。 You can't multiply that sequence by a float (0.1). 您不能将该序列乘以float(0.1)。 It looks like what you wanted to put there was i . 看起来你想放在那里的是i

Incidentally, i is not a great name for that variable. 顺便说一句, i不是那个变量的好名字。 Consider something more descriptive, like growthRate or rate . 考虑更具描述性的内容,例如growthRaterate

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. 当您尝试将一个浮点乘以growRates(这是一个列表)时,您会收到该错误。

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). 因为growthRates是一个序列(你甚至迭代它!)并且你将它乘以(1 + 0.01),这显然是一个浮点数(1.01)。 I guess you mean for growthRate in growthRates: ... * growthrate ? 我猜你的意思for growthRate in growthRates: ... * growthrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM