**Python common mistakes are **
1. NameError variable name error
2. IndentationError code indentation error
3. AttributeError object attribute error
4. TypeError
5. IOError input and output error
6. KeyError dictionary key value error
Specific introduction
Error:
printa
Traceback(mostrecentcalllast):
File"<stdin ",line1,in<module
NameError:name'a'isnotdefined
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,
In other words, you accidentally wrote the wrong variable name.
Note: In Python, there is no need to display variable declaration statements. Variables are automatically declared when they are assigned for the first time.
Recommend to study "python tutorial".
a=1
printa
1
Code
a=1b=2
ifa<b:
printa
Error:
IndentationError:expectedanindentedblock
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
ifa<b:
printa
Error:
importsys
sys.Path
Traceback(mostrecentcalllast):
File"<stdin ",line1,in<module
AttributeError:'module'objecthasnoattribute'Path'
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
['',' /usr/lib/python2.6/site-packages']
Content expansion:
python view error type
‘''
View error type
‘''try:
a =int(input('Please enter the dividend'))
b =int(input('Please enter the divisor'))print(a/b)print('******************')
except Exception as m:print(m)
So far, this article on how to view errors in python is introduced. For more information about viewing errors in python, please search for ZaLou.Cn's previous articles or continue to browse related articles below. I hope you will support ZaLou more in the future. Cn!
Recommended Posts