Source network
Hello everyone , I'm a rookie .
If you say what you fear most about writing code , It is without question Bug. And for the novice , Just came into contact with programming , While enjoying the accomplishment of writing code , It is often used by all kinds of Bug I got dizzy .
today , We did one issue Python Common error reporting sharing , Save your code !
01
The indentation error (IndentationError)
stay Python in , All the code is arranged with the correct spaces . therefore , Whether it's more space , Still missing space , The whole code won't run , And only return an error function .
Python Code follows PEP8 Blank specification , Each level of indentation uses 4 A space .
The wrong sample
a=1
b=2
if a<b:
print a
correct
a=1
b=2
if a<b:
print a
02
Tab Mixed with Spaces (TabError)
This type of error is caused by the use of both tab and space encoding ,tab Keys are essentially tabs , Instead of the indent . Because the width of the space represented by tabs in different text editors is different , Suggest Use spaces .
03
Grammar mistakes (SyntaxError)
There are three reasons for syntax errors :
invalid syntax (invalid syntax)
Punctuation missing , Mixed use of Chinese and English symbols , Spelling mistakes , Variable name or function name is used .
Invalid character in identifier (invalid character in identifier)
Unrecognized characters appear in the code , Check whether there are redundant characters or Chinese characters .
Incomplete string detected (EOL while scanning string litera)
In many cases, it is because the quotation marks around the string are not uniform .
The wrong sample
print( 'hello', 'world')
The reason for the error : Comma is Chinese comma
Error message :SyntaxError: invalid character inidentifier
result = (1024+(512*2)/128
The reason for the error : Parentheses do not appear in pairs
Error message :SyntaxError:unexpected EOF whileparsing
if name =="A"
print("hello")
The reason for the error : Forget in if/elif/else/while/for/def/class Add a colon at the end of the statement
Error message :SyntaxError:invalid syntax
03
Variable name error (NameErro)
Variable name error is the most common and common built-in error reporting type , Often appear in Python Variable naming , If the variable cannot be found, it will raise NameError. Rules for variable names , Here are a few things to keep in mind :
Variable names can only contain letters 、 Numbers and underscores , Start of number not available ;
Variable names cannot contain spaces , But you can use an underline to separate the words ;
Don't put Python Keywords and function names are used as variable names , Such as print;
Variable names should be short and descriptive ;
Be careful with lowercase letters l And capital letters O, Because it's easy to be mistaken for numbers 1 and 0.
Variable name error occurred , You can check whether the variable is assigned , Whether the case is inconsistent or the variable name is written incorrectly , Find it and fix it .
The wrong sample
message = "Hello!"
print(mesage)
The reason for the error : Misspelling variable name , Mistakenly massage Spell as masge
Error message :NameError: name 'mesage' is not defined
05
Index error (IndexError)
An index is the position of an item in an array or list , When we try to access elements from a list or tuples from an index that does not exist in the list , This exception will happen .
for example , There is a 10 A list of elements , Index in 0 To 9 Between , If you try to access the index 10 or 11 Or more elements , It will produce IndexError.
The wrong sample
a = [1,2,3]
print(a[3])
The reason for the error : list a The... Does not exist in 4 An index , The index of the list is from 0 Numbered starting
Error message :IndexError: string index out of range
06
Key error (KeyError)
In reading Dictionary key and value when , Such as fruit key non-existent , It will trigger KeyError error .
The wrong sample
d = {'a':1,'b':2}
print(d['f'])
The reason for the error : key ‘f’ non-existent
Error message :KeyError: 'f'
07
Type error (TypeError)
When using an incorrect or unsupported object type in a program , This error will be raised . If you try to call a non callable object or iterate through a non iterative identifier , This error will also be raised .
The wrong sample
age=18
print(" My age is "+age)
The reason for the error : In the use of “+” When splicing , You must use a string , perhaps Put the numbers in str() Function to string
Error message :TypeError:can only concatenate str(not"int")to str
08
Property error (AttributeError)
Property errors are raised when property references and assignments fail .
The cause of this error is an attempt to access an unknown object property , In other words, the attribute of the corresponding object cannot be found . You can check the constructor in the class __init__() Whether it is written correctly , Two underscores on the left and right .
For beginners , Code often appears Bug Doesn't mean you're not good at learning . If one by one Bug As a little monster in the game , Then destroy Bug The process of is not the process of upgrading ?
therefore , Work hard ! eliminate Bug!
All rivers and mountains are always in love , Order one OK? .
Recommended reading :
introduction : The most complete zero Foundation Python The problem of | Zero Basics 8 Months Python | Actual project | learn Python That's the shortcut
dried food : A short comment on crawling Douban , The movie 《 The rest of us 》 | 38 year NBA Best player analysis | From people's expectation to public praise ! Tang Dynasty detective 3 disappointing | Laugh at the story of the new Yitian dragon slaying | Riddle answer King | use Python Make a massive sketch of my little sister | Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Interest : Pinball game | squared paper for practicing calligraphy | Beautiful flowers | Two hundred lines Python《 Cool run every day 》 game !
AI: A robot that can write poetry | Color the picture | Forecast revenue | Mission impossible is so hot , I use machine learning to make a mini recommendation system movie
Gadget : Pdf turn Word, Easily handle forms and watermarks ! | One touch html Save the page as pdf!| bye PDF Withdrawal charges ! | use 90 Lines of code create the strongest PDF converter ,word、PPT、excel、markdown、html One click conversion | Make a nail low-cost ticket reminder ! |60 Line of code to do a voice wallpaper switcher, look at my little sister every day !|
Annual hot money copy
1). Oh my god !Pdf turn Word use Python Easy to handle !
2). learn Python It's delicious ! I use 100 Line of code to make a website , Help people PS Travel pictures , Earn a chicken leg to eat
3). Premiere billions , Hot all over the net , I analyzed 《 My sister 》, Discovered the secrets
4).80 Line code ! use Python Make a dorai A Dream separation
5). What you have to master 20 individual python Code , short , Useful
6).30 individual Python Strange sexual skills Collection
7). I summed up 80 page 《 Rookie Science Python Select dry goods .pdf》, Is dry
8). bye Python! I have to learn Go 了 !2500 Word depth analysis !
9). Found a licking dog welfare ! This Python Reptile artifact is great , Automatically download sister pictures
Click to read the original , see B My station 20 A video !