When I first learned python, it might be a bit complicated to understand the meaning of Python's error message. Here's a list of common run-time errors that make your program crash.
1) forget in if , elif , else , for, while, class ,def Add at the end of the declaration: (causes "syntaxerror:invalid syntax")
The error will occur in code similar to the following:
?
12 |
if spam = = 42 print ( ‘Hello!‘ ) |
2) Use = instead of = = (causes "syntaxerror:invalid syntax")
= is an assignment operator and = = is equal to the comparison operation. This error occurs in the following code:
?
12 |
if spam = 42 : print ( ‘Hello!‘ ) |
3) Incorrect use of indent amount. (causes "indentationerror:unexpected indent", "Indentationerror:unindent does not match any outer Indetationlevel "and"indentationerror:expected an indented block")
Remember that the indent increment is only used after the statement that ends with: and then must revert to the previous indent format. This error occurs in the following code:
?
12345678910111213 |
print
(
‘Hello!‘
)
print
(
‘Howdy!‘
)
或者:
if spam
=
= 42
:
print
(
‘Hello!‘
)
print
(
‘Howdy!‘
)
或者:
if spam
=
= 42
:
print
(
‘Hello!‘
)
|
4) forget to call Len () in the FOR Loop statement (resulting in "TypeError: ' List ' object cannot is interpreted as an integer ")
Usually you want to iterate over an element of a list or string by index, which calls the range () function. Remember to return the Len value instead of returning the list.
This error occurs in the following code:
?
123 |
spam = [ ‘cat‘ , ‘dog‘ , ‘mouse‘ ] for i in range (spam): print (spam[i]) |
5) Attempt to modify the value of string (resulting in "TypeError: ' str ' object doesnot the support item assignment")
A string is an immutable data type that occurs in the following code:
?
123 |
spam = ‘I have a pet cat.‘ spam[ 13 ] = ‘r‘ print (spam) |
And you actually want to do this:
?
123 |
spam = ‘I have a pet cat.‘ spam = spam[: 13 ] + ‘r‘ + spam[ 14 :] print (spam) |
6) attempt to concatenate non-string value with string (resulting in "Typeerror:can ' t convert ' int ' object to str implicitly")
This error occurs in the following code:
?
12 |
numEggs = 12 print ( ‘I have ‘ + numEggs + ‘ eggs.‘ ) |
And you actually want to do this:
?
1234567 |
numeggs = 12 print ( ' I have ' + Code class= "Python functions" >str (Numeggs) + ' eggs. ' ) numeggs = 12 print ( ' I have%s eggs. ' % |
7) forget the quotes in the string (resulting in "syntaxerror:eol while scanning string literal")
This error occurs in the following code:
?
12345678910 |
print (Hello!‘) 或者: print (‘Hello!) 或者: myName = ‘Al‘ print ( ‘My name is ‘ + myName + . How are you?‘) |
8) incorrect spelling of variable or function name (resulting in "nameerror:name ' Fooba 'is not defined")
This error occurs in the following code:
?
12345678910 |
foobar = print ( ' My name is ' + fooba) spam = ruond ( 4.2 spam = round ( 4.2 |
9) method name spelling error (causes "attributeerror: ' str ' object has no attribute ' Lowerr')
This error occurs in the following code:
?
12 |
spam = ‘THIS IS IN LOWERCASE.‘ spam = spam.lowerr() |
Reference exceeds the list maximum index (resulting in "Indexerror:list indexout of range")
This error occurs in the following code:
?
12 |
spam = [ ‘cat‘ , ‘dog‘ , ‘mouse‘ ] print (spam[ 6 ]) |
One ) use a dictionary key value that does not exist (resulting in "keyerror: ' Spam '")
This error occurs in the following code:
?
12 |
spam = { ' cat ' : ' Zophie ' ' dog ' : ' Basil ' ' mouse ' : ' whiskers ' print ( ' the name of my pet zebra is ' + spam[ ' zebra ' ]) |
Try using the Python keyword as the variable name (resulting in "syntaxerror:invalid Syntax")
Python key cannot be used as a variable name, and this error occurs in the following code:
?
Python3 keywords are: And, as, assert, break, class, continue, Def, Del, elif, else, except, False, finally, for, from, Global, if, Import, in, are, lambda, None, nonlocal, not, or, pass, raise, return, True, try, when, with, yield
Use the value added operator in a defined new variable (resulting in "nameerror:name ' Foobar 'is not defined")
Do not use 0 or an empty string as the initial value when declaring a variable, so that using the increment operator's sentence spam + = 1 equals spam = spam + 1, which means that spam needs to specify a valid initial value.
This error occurs in the following code:
?
123 |
spam = 0 spam + = 42 eggs + = 42 |
Use a local variable in the function before defining the local variable (there is a global variable with the same name as the local variable) (resulting in "unboundlocalerror:local variable ' foobar ' referenced Before assignment")
It is very complicated to use a local variable to that in a function and a global variable with the same name at the same time: if anything is defined in the function, it is local if it is used only in the function, and the other is the global variable.
This means that you cannot define it before you use it as a global variable in a function.
This error occurs in the following code:
?
12345 |
someVar = 42 def myFunction(): print (someVar) someVar = 100 myFunction() |
try to use range () to create a list of integers (resulting in "TypeError: ' Range ' object does not support item assignment")
Sometimes you want to get an ordered list of integers, so range () looks like a good way to generate this list. However, you need to remember that range () returns "Range object" instead of the actual list value.
This error occurs in the following code:
?
12 |
spam = range ( 10 ) spam[ 4 ] = - 1 |
Maybe that's what you want to do:
?
12 |
spam = list ( range ( 10 )) spam[ 4 ] = - 1 |
(Note: In python 2, spam = Range (10) is OK because the range () in Python 2 returns a list value, but in Python 3 the above error is generated)
Good at + + or--self-increment decrement operator. (resulting in "syntaxerror:invalid Syntax")
If you are accustomed to other languages such as C + +, Java, PHP, and so on, you might want to try using + + or--self-increment-subtract a variable. There is no such operator in Python.
This error occurs in the following code:
?
Maybe that's what you want to do:
?
() forgot to add the self parameter for the first parameter of the method (resulting in "typeerror:mymethod () takes no arguments (1 given)")
This error occurs in the following code:
?
12345 |
class Foo(): def myMethod(): print ( ‘Hello!‘ ) a = Foo() a.myMethod() |
Original link/oschina.net original translation
Bloomberg
Posted 2 year ago
17 back/18138 reads
Tags:Python Oschina Original Translation
0 Collection (117) Sort By default display of latest comments 17 Comments (last reply: 2 years ago)