程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Common Python errors and solutions, recommended collection

編輯:Python

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 :

  1. invalid syntax (invalid syntax)

    Punctuation missing , Mixed use of Chinese and English symbols , Spelling mistakes , Variable name or function name is used .

  2. Invalid character in identifier (invalid character in identifier)

    Unrecognized characters appear in the code , Check whether there are redundant characters or Chinese characters .

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

04

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!

【python Study 】
learn Python The partners , Welcome to join the new exchange 【 Junyang 】:1020465983
Discuss programming knowledge together , Become a great God , There are also software installation packages in the group , Practical cases 、 Learning materials


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved