How to view errors in python

**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

  1. NameError variable name error

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
  1. IndentationError code indentation error

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
  1. AttributeError object attribute error

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(&#39;Please enter the dividend'))
b =int(input(&#39;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

How to view errors in python
How to custom catch errors in python
How to view installed modules in python
python how to view webpage code
How to wrap in python code
How to omit parentheses in Python
How to write classes in python
How to filter numbers in python
How to read Excel in Python
How to write return in python
How to view the python module
How to understand variables in Python
How to clear variables in python
How to use SQLite in Python
How to use and and or in Python
How to introduce third-party modules in Python
How to represent null values in python
How to write win programs in python
How to install third-party modules in Python
How to write try statement in python
How to define private attributes in Python
How to understand global variables in Python
How to open python in different systems
How to sort a dictionary in python
How to add background music in python
How to represent relative path in python
How to use the round function in python
How to program based on interfaces in Python
How to install python in ubuntu server environment
How to use code running assistant in python
Teach you how to write games in python
How to delete files and directories in python
How to install the downloaded module in python
How to write a confession program in python
How to perform continuous multiplication calculation in python
How to comment python code
How to learn python quickly
How to uninstall python plugin
How to understand python objects
How to use python tuples
How to view detailed network routing table in Ubuntu
How to understand the introduction of packages in Python
Example of how to automatically download pictures in python
How to save IE as an attachment in python
How to create a Python virtual environment in Ubuntu 14.04
How to use hanlp in ubuntu
How to use python thread pool
How to find the area of a circle in python
How to install PHP7.4 in CentOS
Common errors and solutions in python
How to save the python program
How to install mysql in Ubuntu 14.04
How to install Python 3.8 on CentOS 8
How to install Python 3.8 on Ubuntu 18.04
How to install Python on CentOS 8
How to install HDP2.6 in Centos7.2
How to solve python dict garbled
How to install mysql in Ubuntu 14.04
Do you still know how to draw cakes in python? ? ?
How to understand python object-oriented programming
How to install Android SDK in centos7