1) forget to add (resulting in "SyntaxError: invalid syntax") at the end of the if, elif, else, for, while, class, and def declaration ")
This error will occur in code similar to the following:
Copy codeThe Code is as follows: if spam = 42
Print ('Hello! ')
2) use = instead of = (resulting in "SyntaxError: invalid syntax ")
= Is the value assignment operator and = is a comparison operation. This error occurs in the following code:Copy codeThe Code is as follows: if spam = 42:
Print ('Hello! ')
3) The number of indentions is incorrect. (Resulting in "IndentationError: unexpected indent", "IndentationError: unindent does not match any outer indetation level", and "IndentationError: expected an indented block ")
Remember to add indentation only after the statement ending with:, and then it must be restored to the original indent format. This error occurs in the following code:Copy codeThe Code is as follows: print ('Hello! ')
Print ('Howdy! ')
Or:
If spam = 42:
Print ('Hello! ')
Print ('Howdy! ')
Or:
If spam = 42:
Print ('Hello! ')
4) forget to call len () in the for loop Statement (resulting in "TypeError: 'LIST' object cannot be interpreted as an integer ")
Generally, You Want To iterate a list or string element through an index. You need to call the range () function. Remember to return the len value instead of the list.
This error occurs in the following code:Copy codeThe Code is as follows: spam = ['cat', 'dog ', 'mouse']
For I in range (spam ):
Print (spam [I])
5) try to modify the string value (resulting in "TypeError: 'str' object does not support item assignment ")
String is an unchangeable data type. This error occurs in the following code:Copy codeThe Code is as follows: spam = 'I have a pet cat .'
Spam [13] = 'R'
Print (spam)
But you actually want to do this:Copy codeThe Code is as follows: spam = 'I have a pet cat .'
Spam = spam [: 13] + 'R' + spam [14:]
Print (spam)
6) try to connect a non-string value with a string (resulting in "TypeError: Can't convert 'int' object to str implicitly ")
This error occurs in the following code:Copy codeThe Code is as follows: numEggs = 12
Print ('I have' + numEggs + 'eggs .')
But you actually want to do this:Copy codeThe Code is as follows: numEggs = 12
Print ('I have' + str (numEggs) + 'eggs .')
Or:
NumEggs = 12
Print ('I have % s eggs.' % (numEggs ))
7) add quotation marks at the beginning and end of the string (resulting in "SyntaxError: EOL while scanning string literal ")
This error occurs in the following code:Copy codeThe Code is as follows: print (Hello! ')
Or:
Print ('Hello !)
Or:
MyName = 'al'
Print ('My name is '+ myName +. How are you? ')
8) misspelling of variables or function names (resulting in "NameError: name 'fooba' is not defined ")
This error occurs in the following code:Copy codeThe Code is as follows: foobar = 'al'
Print ('My name is '+ fooba)
Or:
Spam = ruond (4.2)
Or:
Spam = Round (4.2)
9) incorrect method name spelling (resulting in "AttributeError: 'str' object has no attribute 'lowerr '")
This error occurs in the following code:Copy codeThe Code IS as follows: spam = 'this is in lowercase .'
Spam = spam. lowerr ()
10) Reference exceeds the maximum list index (resulting in "IndexError: list index out of range ")
This error occurs in the following code:Copy codeThe Code is as follows: spam = ['cat', 'dog ', 'mouse']
Print (spam [6])
11) use a non-existent dictionary key value (resulting in "KeyError: 'spam '")
This error occurs in the following code:Copy codeThe Code is as follows: spam = {'cat': 'zophie ', 'dog': 'basil ', 'mouse': 'whiskers '}
Print (The name of my pet zebra is '+ spam ['zebra'])
12) try to use the Python keyword as the variable name (resulting in "SyntaxError: invalid syntax ")
The key to Python cannot be used as a variable name. This error occurs in the following code:
Copy codeThe Code is as follows: class = 'algebr'
Python3 keywords include: and, as, assert, break, class, continue, def, del, elif, else, else t, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
13) use the value-added operator in a new variable definition (resulting in "NameError: name'foobar' is not defined ")
Do not use 0 or an empty string as the initial value when declaring a variable. In this way, the use of the auto-increment operator spam + = 1 equals spam = spam + 1, this means that spam needs to specify a valid initial value.
This error occurs in the following code:Copy codeThe Code is as follows: spam = 0
Spam + = 42
Eggs + = 42
14) use a local variable in the function before defining the local variable (a global variable with the same name as the local variable exists) (resulting in "UnboundLocalError: local variable 'foobar' referenced before assignment ")
It is very complicated to use local variables in a function and there are global variables with the same name at the same time. The rule is as follows: if a function defines anything, if it is used only in functions, it is local, and vice versa.
This means that you cannot use it as a global variable in the function before defining it.
This error occurs in the following code:Copy codeThe Code is as follows: someVar = 42
Def myFunction ():
Print (someVar)
Somevar= 100
MyFunction ()
15) Try to use range () to create an integer list (resulting in "TypeError: 'range' object does not support item assignment ")
Sometimes you want to get an ordered integer list, so range () seems to be 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:Copy codeThe Code is as follows: spam = range (10)
Spam [4] =-1
Maybe this is what you want to do:Copy codeThe Code is as follows: spam = list (range (10 ))
Spam [4] =-1
(Note: In Python 2, spam = range (10) can work, because range () in Python 2 returns the list value, but the above errors will occur in Python 3)
16) Not bad in ++ or -- auto-incrementing auto-subtraction operator. (Cause "SyntaxError: invalid syntax ")
If you are used to other languages such as C ++, Java, and PHP, you may want to use ++ or -- auto-increment to subtract a variable. There is no such operator in Python.
This error occurs in the following code:Copy codeThe Code is as follows: spam = 1
Spam ++
Maybe this is what you want to do:Copy codeThe Code is as follows: spam = 1
Spam + = 1
17) forgot to add the self parameter to the first parameter of the method (resulting in "TypeError: myMethod () takes no arguments (1 given )")
This error occurs in the following code:Copy codeThe Code is as follows: class Foo ():
Def myMethod ():
Print ('Hello! ')
A = Foo ()
A. myMethod ()