问题
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
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
Now I am getting this error.
Exception Type: NameError
Exception Value:
name 'a' is not defined
Then i removed the global line and just put this
a = "P"
and got the following error
str object has no attribute length() or 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.
回答1:
Python String len() Method
Syntax
Following is the syntax for len() method −
len( str )
Example
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
Reference: Python String len() Method
UnboundLocalError: local variable 'a' referenced before assignment
Explanation
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.
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.
Example
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
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()
Then it works fine.
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()
Reference: See this SO answer.
NameError: name 'a' is not defined
Explanation
Probably you are getting this error because in Python, you cannot compare two strings using equals() method. There is no such method exists.
Example: Comparing two strings
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
回答2:
I assume you've come from a language such as Java where you call functions directly on strings. You could do that in Python:
>>> A = "Hello"
>>> B = "Hello"
>>> A.__len__()
5
>>> A.__eq__(B)
True
However, the proper way of doing it is like so:
>>> len(A)
5
>>> A == B
True
With that being said, I'm not sure what you're trying to accomplish with the length function there. You said your code is checking whether the first 3 letters of the 2 strings are the same - you can do that like so:
A = "car123"
B = "car456"
print(A[:3] == B[:3]) # -> True
This uses Python's slice notation to get the first 3 characters of each string, and compares these slices with each other.
来源:https://stackoverflow.com/questions/41368066/str-object-has-no-attribute-len