**Python common mistakes are **
NameError variable name error
IndentationError code indentation error
AttributeError object attribute error
Detailed explanation
1. NameError variable name error
Error:
print a<br Traceback(most recent call last):<br File "<stdin ", line 1,in<module <br NameError: name 'a' is not defined<br
solution:
First assign a value to a. To use it. In the actual coding process, when a NameError error is reported, check whether the variable is assigned a value, or whether there is a case inconsistency error, or the variable name is accidentally written wrong.
Note: In Python, there is no need to display variable declaration statements. Variables are automatically declared when they are assigned for the first time.
a=1<br print a<br 1<br
2. IndentationError code indentation error
Code
a=1b=2<br if a<b:<br print a<br
Error:
IndentationError: expected an indented block<br
the reason:
The indentation is wrong. Python's indentation is very strict. If there are multiple spaces at the beginning of a line, an error will be reported if there are fewer spaces. This is a mistake often made by novices because they are not familiar with python coding rules. Code blocks like def, class, if, for, while, etc. need to be indented.
The indentation is four spaces wide. One point needs to be explained. Different text editors have different space widths represented by tab characters. If the code needs to be read and written across platforms or editors, it is recommended not to use tabs .
solution
a=1b=2<br if a<b:<br print a<br
3. AttributeError object attribute error
Error:
import sys<br sys.Path<br Traceback(most recent call last):<br File "<stdin ", line 1,in<module <br AttributeError:'module' object has no attribute 'Path'<br
the reason:
The sys module has no Path attribute.
Python is case sensitive, and Path and path represent different variables. Just change Path to path.
sys.path<br ['','/usr/lib/python2.6/site-packages']<br
Examples of errors encountered by beginners:
Use wrong indentation
Python uses indentation to distinguish code blocks. Common misuses:
print('Hello!')print('Howdy!')
Causes: IndentationError: unexpected indent. Each line of code in the same code block must be indented consistently
if spam ==42:print('Hello!')print('Howdy!')
Causes: IndentationError: unindent does not match any outer indentation level. After the code block ends, the indentation is restored to the original position
if spam ==42:print('Hello!')
Causes: IndentationError: expected an indented block, indentation should be used after ":"
Variable is not defined
if spam ==42:print('Hello!')
Causes: NameError: name'spam' is not defined
Get the index position of the list element and forget to call the len method
When getting elements by index position, forget to use the len function to get the length of the list.
spam =['cat','dog','mouse']for i inrange(spam):print(spam[i])
Cause: TypeError: range() integer end argument expected, got list. The correct approach is:
spam =['cat','dog','mouse']for i inrange(len(spam)):print(spam[i])
Of course, a more Pythonic way is to use enumerate
spam =['cat','dog','mouse']for i, item inenumerate(spam):print(i, item)
Used before local variable assignment in function
someVar =42
def myFunction():print(someVar)
someVar =100myFunction()
Causes: UnboundLocalError: local variable'someVar' referenced before assignment
When a function has a variable with the same name in the global scope, it will look for the variable in the order of LEGB. If a variable with the same name is also defined in the local scope inside the function, it will no longer go to the outer scope Looked up. Therefore, someVar is defined in the myFunction function, so print(someVar) is no longer looked up outside, but the variable has not been assigned during print, so UnboundLocalError appears
The above is the details of common errors in python and their solutions. For more information about common errors in python, please pay attention to other related articles on ZaLou.Cn!
Recommended Posts