'str'对象没有属性'len' [英] 'str' object has no attribute 'len'
问题描述
通过检查前三个字母/数字并确保它们相同之前,我已经有了一种可以正常工作的方法
Ive got a method that use to work by checking the first three letters/numbers and making sure they are the same before it continues like so
def combineProcess(request):
carID1 = request.POST['carID1']
carID2 = request.POST['carID2']
for x in range (0,3):
a += carID1.length(x)
b += carID2.length(x)
if a.equals(b):
//do something
在使用之前,现在它停止了,并且出现了这个错误.
before it use to work now it stopped and i get this error.
Exception Type: UnboundLocalError
Exception Value:
local variable 'a' referenced before assignment
几周前我从未使用过它,但是并没有改变任何东西,所以我将a和b设置为全局.
which i never use to get a few weeks ago didnt change anything so i made a and b global.
def combineProcess(request):
carID1 = request.POST['carID1']
carID2 = request.POST['carID2']
global a,b
for x in range (0,3):
a += carID1.length(x)
b += carID2.length(x)
if a.equals(b):
//do something
现在我收到此错误.
Exception Type: NameError
Exception Value:
name 'a' is not defined
然后我删除了全局行,并把它放在了
Then i removed the global line and just put this
a = "P"
并出现以下错误
str object has no attribute length() or len()
现在让我感到困惑的是,这段代码如何停止工作,为什么不能识别字符串对象具有len()方法.主要是我迷失了我的代码从工作到两周不工作的过程.
which now has me puzzled how has this code stop working and why cant it recognize that a string object has a len() method. mainly I am lost how my code went from working to not working over a two weeks off.
推荐答案
Python字符串len()方法
Python String len() Method
语法
以下是len()方法的语法-
Following is the syntax for len() method −
len( str )
示例
str = "this is string example....wow!!!";
print("Length of the string: ", len(str))
当我们运行上面的程序时,它会产生以下结果-
When we run above program, it produces following result −
Length of the string: 32
参考: Python字符串len()方法
UnboundLocalError:分配前引用了本地变量'a'
UnboundLocalError: local variable 'a' referenced before assignment
说明
这是因为,即使存在a
和b
,您也正在函数combineProcess()
中使用名称为a
和b
的赋值语句.自然地,这会在函数范围内创建一个名为a
和b
的变量.
This is because, even though a
and b
exists, you're also using an assignment statement on the name a
and b
inside of the function combineProcess()
. Naturally, this creates a variable inside the function's scope called a
and b
.
Python解释器会在模块加载时看到此情况,并决定在本地范围内不应使用a
和b
的全局范围,这会导致在尝试引用变量之前出现问题本地分配.
The Python interpreter sees this at module load time and decides that the global scope's of a
and b
should not be used inside the local scope, which leads to a problem when you try to reference the variable before it is locally assigned.
示例
Var1 = 1
Var2 = 0
def function():
if Var2 == 0 and Var1 > 0:
print("Result One")
Var1 =- 1
function()
如果运行此程序,则会出现以下错误.
If you run this program, it gives the following error.
UnboundLocalError: local variable 'Var1' referenced before assignment
由于修改了Var1
的值,因此将在函数范围内创建一个名为Var1
的变量.结果由于条件检查,在Var1 =- 1
语句之前,报告了Var1 > 0
错误.
Since, the value of Var1
is modified, this creates a variable inside the function's scope called Var1
. As a result error is reported because of the condition check, Var1 > 0
before the Var1 =- 1
statement.
但是,如果我们按如下所示修改代码.
But if we modify the code as follows.
Var1 = 1
Var2 = 0
def function():
global Var1
if Var2 == 0 and Var1 > 0:
print("Result One")
Var1 =- 1
function()
然后正常工作.
请注意,如果我们将语句Var1 =- 1
移到if
条件检查之前,那么即使您不使用global Var1
语句,它也不会报告任何错误.因此,以下代码可以正常工作.
Note that, if we move the statement Var1 =- 1
before the if
condition check, then it will not report any error even if you don't use global Var1
statement. So, the following code works fine.
Var1 = 1
Var2 = 0
def function():
Var1 =- 1
if Var2 == 0 and Var1 > 0:
print("Result One")
function()
参考:请参见 answer .
NameError:名称'a'未定义
NameError: name 'a' is not defined
说明
您可能会收到此错误,因为在Python中,您无法使用equals()
方法比较两个字符串.没有这样的方法.
Probably you are getting this error because in Python, you cannot compare two strings using equals()
method. There is no such method exists.
示例:比较两个字符串
您可以使用> , < , <= , <= , == , !=
比较两个字符串. Python按字典顺序比较字符串,即使用字符的ASCII值.例如,要比较两个字符串是否相等,可以执行以下操作.
You can use > , < , <= , <= , == , !=
to compare two strings. Python compares string lexicographically i.e using ASCII value of the characters. For example, to compare two strings for equality, you can do as follows.
if string1 == string2:
// do something
这篇关于'str'对象没有属性'len'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!