17 Common Python Runtime Errors for Beginners
Translate _Dream_Chaser
This article is transferred from the network, if it involves infringement, please contact us in time
It can still be difficult for beginners to figure out some of Python's error messages, and the following is a list of some common runtime errors.
1) Forgot to add : at the end of if , elif , else , for , while , class , def statements (resulting in "SyntaxError: invalid syntax")
The error will occur in a code similar to the following.
2) Use = instead of == (causes "SyntaxError: invalid syntax")
= is an assignment operator and == is an equals comparison operation. The error occurs in the following code.
3) Wrong amount of indentation is used. (resulting in "IndentationError: unexpected indent", "IndentationError: unindent does not match any outer indetation level", and "IndentationError: expected an indented block")
Remember that the indent increase is only used when starting with: After the ending statement, And after that you must revert to the previous indent format。 The error occurs in the following code.
4)Forgot to call len() in the for loop statement (resulting in "TypeError: 'list' object cannot be interpreted as an integer")
Usually you want to iterate over the elements of a list or string by index, which requires calling the range() function. Remember to return the len value instead of returning this list.
The error occurs in the following code.
5) Trying to modify the value of string (resulting in "TypeError: 'str' object does not support item assignment")
string is an immutable data type, The error occurs in the following code.
And you actually want to do this.
6) Trying to concatenate a non-string value with a string (resulting in "TypeError: Can't convert 'int' object to str implicitly")
The error occurs in the following code.
And you actually want to do this.
Or.
7) Forgot to put quotes at the beginning and end of a string (resulting in "SyntaxError: EOL while scanning string literal")
The error occurs in the following code.
Or:
Or:
8) Misspelling of variable or function names (resulting in "NameError: name 'fooba' is not defined")
The error occurs in the following code.
9) Wrong spelling of method name (resulting in "AttributeError: 'str' object has no attribute 'lowerr'")
The error occurs in the following code.
10) Reference exceeds list maximum index (causes "IndexError: list index out of range")
The error occurs in the following code.
11) Using a non-existent dictionary key value (resulting in "KeyError: 'spam'")
The error occurs in the following code.
12) Trying to use Python keywords as variable names (resulting in "SyntaxError: invalid syntax")
Python The key cannot be used as a variable name, The error occurs in the following code.
Python3 The keywords are:and, as, assert, break, class, continue, def, del, elif, else, except, False, finally, for, from, global, if, import, in, is, lambda, None, nonlocal, not, or, pass, raise, return, True, try, while, with, yield
13) Using the value-added operator in a definition of a 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 the sentence spam += 1 with the self-increment operator is equivalent to spam = spam + 1, which means that spam needs to specify a valid initial value.
The error occurs in the following code.
14)Using a local variable in a function before defining it (when a global variable with the same name as the local variable exists) (resulting in "UnboundLocalError: local variable 'foobar' referenced before assignment")
Using a local variable in a function to that while there is a global variable of the same name is complicated, the rule of use is that if anything is defined in a function, if it is only used in the function then it is local, and vice versa it is a global variable.
This means you can't use it as a global variable in a function before you define it.
The error occurs in the following code.
15) Trying to create a list of integers using range() (leads to "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 a "range object" and not the actual list value.
The error occurs in the following code.
Maybe that's what you want to do.
(Note: spam = range(10) works in Python 2 because range() returns list values in Python 2, but in Python 3 it produces the above error)
16) Not bad for the ++ or - self-increasing and self-subtracting operators. (resulting in "SyntaxError: invalid syntax")
If you are used to other languages such as C++ , Java , PHP etc., you may want to try using ++ or - to increment and decrement a variable. There is no such operator in Python.
The error occurs in the following code.
Maybe that's what you want to do.
17) Forgot to add the self argument to the first argument of the method (resulting in "TypeError: myMethod() takes no arguments (1 given)")
The error occurs in the following code.