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
Similar Posts:
- The solution to the problem of unified does not match any outer indentation level in pychar platform
- [733]TabError: inconsistent use of tabs and spaces in indentation
- IndentationError: unexpected indent python
- Solution of syntax error: invalid character in identifier when writing Python code
- How to Solve pylint error tab indent
- Improper indentation of Python comments results in an indentation error: unexpected indent
- Pep8: continuation line over indented for visual indent appears when wrapping Python code
- Expected indentation of 8 spaces but found [How to Solve]
- Python–TabError: inconsistent use of tabs and spaces in indentation
- Python3.x Run Python2.x Codes syntax error: “Missing parentheses in call to ‘print’