Python 类型错误:“int”类型的对象不需要 len() 错误帮助
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35204529/
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
TypeError: object of type 'int' has no len() error assistance needed
提问by Luke Hymanson
I am writing a piece of code for my code that when the user inputs 7 digits it multiplies the digits by 3 and 1 respectively. Here is the code;
我正在为我的代码编写一段代码,当用户输入 7 位数字时,它分别将数字乘以 3 和 1。这是代码;
When it goes to check if the user has entered 7 digits it gives me this error: TypeError: object of type 'int' has no len()
当它检查用户是否输入了 7 位数字时,它给了我这个错误:TypeError: object of type 'int' has no len()
回答by Idos
Well, maybe an int
does not posses the len
attribute in Python like your error suggests?
好吧,也许 anint
没有len
像您的错误所暗示的那样在 Python 中拥有该属性?
Try:
尝试:
len(str(numbers))
回答by e.doroskevic
Abstract:
抽象的:
The reason why you are getting this error
message is because you are trying to call a method on an int
type of a variable. This would work if would have called len()
function on a list
type of a variable. Let's examin the two cases:
您收到此error
消息的原因是因为您正尝试对int
变量类型调用方法。如果len()
在list
变量类型上调用函数,这将起作用。让我们来看看这两种情况:
Fail:
失败:
num = 10
print(len(num))
The above will produce an error similar to yours due to calling len()
function on an int
type of a variable;
由于len()
在int
变量类型上调用函数,上述将产生与您类似的错误;
Success:
成功:
data = [0, 4, 8, 9, 12]
print(len(data))
The above will work since you are calling a function on a list
type of a variable;
由于您正在对list
变量类型调用函数,因此上述方法将起作用;
回答by bob marti
May be it is the problem of using len()
for an integer value.
does not posses the len attribute in Python.
可能是使用len()
整数值的问题。在 Python 中不具有 len 属性。
Error as:I will give u an example:
错误为:我会给你一个例子:
number= 1
print(len(num))
Instead of use ths,
而不是使用 ths,
data = [1,2,3,4]
print(len(data))