sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print(sum)
The code is as above, but an error is reported during operation:
It is found that the indentation is missing, and it is corrected as follows:
sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print(sum)
No problem
Run as:
However, if the code format is corrected:
sum = 0 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: sum = sum + x print(sum)
Output is:
It can be seen here that the most distinctive feature of Python is to write modules with indentation
The number of indented whitespace is variable, but all code block statements must contain the same number of indented whitespace, which must be strictly enforced
indentationerror: indent does not match any outer indentation level the error indicates that the indenting methods you use are inconsistent. Some are tab indenting and some are space indenting. Just change to consistent
If so IndentationError: unexpected indent If there is an error, the python compiler is telling you “Hi, man, the format in your file is wrong. It may be that the tab and space are not aligned”. All Python have very strict format requirements
Therefore, the same number of line indentation spaces must be used in Python code blocks
It is recommended that you use at each indentation level single tab or two spaces or four spaces , Remember not to mix
Indenting the same set of statements constitutes a code block, which we call a code group
For compound statements such as if, while, def and class, the first line starts with a keyword and ends with a colon (:), and one or more lines of code after this line form a code group
We call the first line and the following code group a clause
So the last two runs with different indents have different results. Because the indentation is different, the code block is different