Common errors and solutions in python

**Python common mistakes are **

  1. NameError variable name error

  2. IndentationError code indentation error

  3. 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

Common errors and solutions in python
CentOS common exceptions and solutions
Generators and iterators in Python
How to view errors in python
Common exceptions and solutions in the use and development of Ubuntu system
How to use and and or in Python
How to custom catch errors in python
Python and scrapy deployment in centos environment
Functions in python
Python and Go
Errors and solutions for installing remix-ide on CentOS
The meaning and usage of lists in python
How to delete files and directories in python
Python introspection and reflection
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Common issues in IDC computer room and Centos8 installation
Installation and use of GDAL in Python under Ubuntu
[python] python2 and python3 under ubuntu
Concurrent requests in Python
Install python in Ubuntu
Context management in Python
Arithmetic operators in python
Python deconstruction and packaging
Write gui in python
Python3 configuration and entry.md
MongoDB usage in Python
Common exceptions and solutions for Ubuntu system installation and configuration
09. Common modules of Python3
Str string in Python
Computational Geometry in Python
Python automated operation and maintenance 2
Concurrent requests in Python (part 2)
Python introduction and environment installation
Python know crawler and anti crawler
Subscripts of tuples in Python
centos7 install python3 and ipython
Python common data structure collation
Talking about inheritance in Python
2020--Python grammar common knowledge points
Centos 6.10 reinstall python and yum
Noteworthy update points in Python 3.9
Python open read and write
CentOS7 install python3 and pip3
Python automated operation and maintenance 1
Python data structure and algorithm
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
Containerize Python applications in 3 minutes
What is introspection in python
CentOS 6 compile and install python 3
What is object-oriented in python
Python common exception handling mechanism
Collection of Python Common Modules
Talking about strings in Python