Recently in with the previous classmates to learn python, progress together, found that many test students in the beginning of Python is easy to make some mistakes, deliberately summed up a bit. In fact, these errors are not only encountered when learning Python, but also when studying other languages.
Error 1: Indentation
Python is a forced indentation of the language, many students in the first contact with Python may not be used to, indentation is always made mistakes. Like what
Python 2.7.12 (default, Sep 17 2016, 13:47:40)[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> good = "ok" File "<stdin>", line 1 good = "ok" ^IndentationError: unexpected indent
This error is usually prompted IndentationError
, and the problem can be solved quickly based on the line of error.
Error 2: Chinese and English symbols
Many beginners do not distinguish between Chinese and English symbols, for example, :
this is a colon in English, :
and this colon is in Mandarin. At first glance looks similar, but actually different characters. Like what
>>> x = 0>>> if x == 1: File "<stdin>", line 1 if x == 1: ^SyntaxError: invalid syntax
The above error is obvious, syntax error, colon is problematic. As a beginner, you may need to be wary of some of the following symbols as they are entered
- English comma
- English Colon
- English parentheses
- Brackets in English
The symbol is not accurate, the general Python interpreter will prompt SyntaxError
, encounter this error do not fear, find a way to solve the good.
Error 3: Spelling error
Because our native language is not English, many times we will not be aware of the spelling mistakes of the words. Like what
>>> word = "hello world">>> print(world)Traceback (most recent call last): File "<stdin>", line 1, in <module>NameError: name 'world' is not defined
The above situation Python complains: world
This variable is not defined, obviously we define the variable name is word
. Some students will even be hello world
wrong to spell it hello word
, visible to Microsoft's office suite Love deep.
Error 4: code block forgot Colon
A lot of Python code block structure is required colon, this I was very not used to, always forget to write, such as
>>> if 1 == 2 File "<stdin>", line 1 if 1 == 2 ^SyntaxError: invalid syntax
Here, if followed by a colon, leads to the following code block. When you forget a colon, you usually report a grammatical error.
Error 5:ide syndrome
Some students like to use the IDE from the beginning, because some good IDE is a syntax error, so when you hit the wrong, the code editor may be flooded with red or yellow wavy lines, but sometimes these lines may not be very accurate, causing everyone a bit unprepared, dare not trust the Bold knock Code, Gradually the frustration accumulated, and then gave up.
In addition, many students like to run Python code directly within the IDE, resulting in a lot of time to pass the argument inconvenient, unable to get the desired results.
In fact, the novice can first soil a little, try not to use the IDE, and so skilled some of it is not too late.
Error 6: Only read or write or not read
Some students only read, do not write code, results in addition to doubts more and more, the code ability to improve speed is actually limited
Some students only write code, do not read the code written by others, the results of some common routines will not, progress speed is limited
Code in fact to see more, look at others write code, Python standard library code although not easy, but you can look at GitHub, some open-source projects, can also find some simple to see, write some can let their sense of accomplishment code, from the simple beginning, gradually complex, gradual.
Well, after reading this article, you should know these points.
- Syntaxerror:invalid syntax: This is a syntax error
- Indentationerror:unexpected indent: This is an indent error
- Nameerror:name ' world ' was not defined: This is a variable not defined
- It's okay to make a mistake.
- Code to read and write more
- It's hard to begin with, until the end.
Small white Learn python the most easy to commit 6 errors, see how many you have encountered