简体   繁体   English

错误无法将序列乘以'float'类型的非整数

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

I'm getting the following error: 我收到以下错误:

     if len(new_send_times) > rate_limit * window * self._throttle:
        TypeError: can't multiply sequence by non-int of type 'float'

This is the code: 这是代码:

if len(new_send_times) > rate_limit * window * self._throttle:
                    # Sleep the remainder of the window period.
                    delta = now - new_send_times[0]
                    total_seconds = (delta.microseconds + (delta.seconds +
                                                           delta.days * 24 * 3600) * 10 ** 6) / 10 ** 6
                    delay = window - total_seconds
                    if delay > 0:
                        sleep(delay)

                recent_send_times.append(now)
                # end of throttling

the values it uses are 它使用的值是

if len([]) > 1.0 * 2.0 * 0.5:

Update: I changed it to below and it works, but I still don't understand the error or if want I'm doing it correct: 更新:我将其更改为以下内容,并且可以使用,但是我仍然不明白该错误,或​​者如果我想做对了,请执行以下操作:

 if len(new_send_times) > float(rate_limit) * float(window) * float(self._throttle):

The error is pretty self-explonatory. 该错误是不言自明的。 You are performing operation in the form 您正在执行以下形式的操作

list * float # or other sequence * float

which is forbidden in python, you can python禁止的,你可以

numeric * float # "traditional" multiplication

or 要么

list * int # creates concatenated copies of the list provided

So investigate your code, check which object is a list and convert it to the number, as the code logic suggests that this is your intended behaviour. 因此,请检查您的代码,检查哪个对象是列表并将其转换为数字,因为代码逻辑表明这是您的预期行为。

Examples: 例子:

[1] * 3 = [1,1,1]

[1] * 1.5 # ERROR

1 * 1.5 == 1.5

float([1]) * 1.5 == 1.5

"1" * 1.5 # ERROR

float("1") * 1.5 == 1.5

The error message 错误讯息

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

tells you that at least one of 告诉你至少有一个

len(new_send_times), rate_limit, window, self._throttle

is not a number, as you suspect, but some kind of sequence. 正如您所怀疑的,它不是数字,而是某种顺序。

The fact that you print these values and see what you expect, along with the fact that using float() on the values makes the code work, suggests strongly that one of them is a string representation of a number (eg '1.0' ) rather than the number itself - str in Python counts as a sequence . print这些值并查看期望值的事实,以及对这些值使用float()使代码正常工作的事实,强烈表明其中之一是数字的字符串表示形式(例如'1.0' ),而不是比数字本身str中的str算作一个sequence

len() will always return an int (or raise an error!), so there's no need to worry about that, but one of the other three is probably a string. len()将始终返回int (或引发错误!),因此无需担心,但是其他三个之一可能是字符串。 You need to source where the values are coming from ( [raw_]input ? Parsed file?) and make sure they're being converted to numerical types at the appropriate point (the earlier the better, as in other contexts you may get more subtle problems). 您需要确定值的来源( [raw_]input ?已解析的文件?),并确保在适当的位置将它们转换为数值类型(越早越好,因为在其他情况下,您可能会变得更微妙)问题)。

To tell the difference, when print ing the values to check, you could do eg 为了区别,在print要检查的值时,您可以执行例如

print(rate_limit, type(rate_limit))

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

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