The code is double-clicking wrong. Python 17 common mistakes are not only for newcomers, you may also

For beginners, it's still difficult to understand some of Python's error messages. Here are some common runtime errors:

1. Forgot to add a colon (:) at the end of the if, elif, else, for, while, class, def statement, resulting in: "SyntaxError: invalid syntax" error

The error occurs in a similar code like: if spam == 42print('Hello!')

2. Use the = sign instead of the == sign, resulting in a "SyntaxError: invalid syntax" error

"=" is an assignment statement, and the "==" sign compares whether the two values ​​are equal. This error often occurs in a similar code like: if spam = 42:print('Hello!')

3. Using the indentation error, resulting in "IndentationError: unexpectedn indent", "IndentationError: unindent does not match any outer indetation level" and "IndentationError: expected an indented block" error

The thing to remember is that indentation occurs only after the line ending with a colon (:), and after the end of this paragraph, you must revert to the previous indentation format. This error often occurs in the following code:

Print('Hello!')print('Howdy!')...and this:

If spam == 42:print('Hello!')print('Howdy!')...and this:

If spam == 42:print('Hello!')

4. Forgetting to call len() in a for loop statement causes "TypeError: 'list' object cannot be interpreted as an integer" error

You want to iterate over a list or string element by index, then you need to call the range() function. But the range function accepts only numbers, such as the return value of len(), not a list. This error often occurs in the following code: spam = ['cat', 'dog', 'mouse']for i in range(spam):print(spam[i])

5. Try to modify the value of string, resulting in "TypeError: 'str' object does not support item assignment" error

String is an immutable data type that often occurs in the following code: spam = 'I have a pet cat.'spam[13] = 'r'print(spam)

6. Try to connect non-string values ​​with strings, resulting in "TypeError: Can't convert 'int' object to str implicitly" error

Errors often occur in the following code: numEggs = 12print('I have ' + numEggs + ' eggs.')

And you actually want to do this: numEggs = 12print('I have ' + str(numEggs) + ' eggs.')

Or: numEggs = 12print('I have %s eggs.' % (numEggs))

7. Forget the quotes at the beginning and end of the string, resulting in "SyntaxError: EOL while scanning string literal" error

This error occurs in the following code: print(Hello!')

Or: print('Hello!)

Or: myName = 'Al'print('My name is ' + myName + . How are you?')

8. The variable or function name is misspelled, resulting in "NameError: name 'fooba' is not defined" error

The error occurs in the following code: foobar = 'Al'print('My name is ' + fooba)

Or: spam = ruond(4.2)

Or: spam = Round(4.2)

9. The method name is misspelled, resulting in "AttributeError: 'str' object has no attribute 'lowerr'" error

This error occurs in the following code: spam = 'THIS IS IN LOWERCASE.'spam = spam.lowerr()

10. The reference list subscript is out of range, resulting in an "IndexError: list index out of range" error.

This error often occurs in the following code: spam = ['cat', 'dog', 'mouse']print(spam[6])

11. Use a dictionary key value that does not exist, resulting in a "KeyError: 'spam'" error

The error occurs in the following code: spam = {'cat': 'Zophie', 'dog': 'Basil', 'mouse': 'Whiskers'}print('The name of my pet zebra is ' + spam[' Zebra'])

12. Try using the Python keyword as the variable name, resulting in a "SyntaxError: invalid syntax" error

The Python keyword cannot be used as a variable name. The error occurs in the following code: class = 'algebra'

The keywords for Python3 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. Use a value-added operator in a new variable definition, resulting in a "NameError: name 'eggs' is not defined" error

Don't assume that a variable uses 0 or an empty string as the initial value when it is created, just use a spam += 1 or spam = spam + 1 of the auto increment operator and spam is a valid initial value that needs to be manually displayed.

This error occurs in the following code: spam = 0spam += 42eggs += 42

14. Using a local variable in a function before defining a local variable (in which case a global variable with the same name as the local variable exists) will result in "UnboundLocalError: local variable 'foobar' referenced before assignment")

The use of local variables in functions, while the existence of the same name global variables is very complicated, the rule is: if you define anything in the function, if it is only used in the function it is local, and vice versa is the global variable .

This means that you can't use it as a global variable in a function before defining it.

The error occurs in the following code: someVar = 42def myFunction():print(someVar)someVar = 100myFunction()

15. Try to create a list of integers using range() (resulting in "TypeError: 'range' object does not support item assignment")

Sometimes you want to get an ordered list of integers, 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: spam = range(10)spam[4] = -1

Maybe this is what you want to do: spam = list(range(10))spam[4] = -1

Note: spam = range(10) is fine in Python 2, because range() returns a list value in Python 2, but the above error occurs in Python 3.

16. Use the ++ or – self-incrementing and decrementing operator, resulting in "SyntaxError: invalid syntax"

If you are used to other languages ​​such as C++, Java, PHP, etc., you may want to try to use ++ or – add and subtract a variable. There is no such operator in Python.

This error occurs in the following code: spam = 1spam++

Maybe this is what you want to do: spam = 1spam += 1

17. Forgot to add a 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: class Foo(): def myMethod():print('Hello!')a = Foo()a.myMethod()

Article source: Baidu Library


Gear Sensor

Gear Sensor has been widely used in the automotive and industrial field, which is important to the measurement of velocity, angel, angular velocity, direction of rotation.

Gear Sensor,Custom Gear Sensor,Gear Sensor 3 Pins,Good Gear Sensor

Yuheng Optics Co., Ltd.(Changchun) , https://www.yhenoptics.com

Posted on