Summary of whitespace and indentation issues in Python

Source: Internet
Author: User

everyone all know that Python is a language that is very sensitive to indentation, the code format is very strict, the personal encounter Some of the most common problems are as follows:

1. Spaces or tabs should not be used where spaces or tab indents are used

2. Different levels of code are not indented

3. The indent quantity is not correct

3.1 Code all use 4 spaces, some code indent is not 4 spaces

3.2 Indent Too much (this should be 4 spaces or 1 tabs, indent 8 spaces or 2 tab) or too little (this should be 8 spaces or 2 tab, indent 4 spaces or 1 tab)

4.themix of tab and space-----The problem is most ugly .

These errors are difficult to discern with the naked eye ...

Error message: indentationerror:unexpected indent;indentationerror:expected an indented block ...

If you encounter this kind of error, It could be the tab and space indentation in the code, and you need to check the tab and space. "

"Solution"

1. Check if a space or tab should not be used, check for indentation, whether the indent is aligned, indent, and whether the space is mixed

2. Annotations are sometimes affected, so the indentation of annotations should also be noticed

3. In particular , if indentation and whitespace are mixed , you can use the text editor notepad++ to display all the spaces or tab characters of the current Python script.

settings See:

Show spaces and tabs, display symbols, view

Tips:

Make sure that you use 4 or 4 of multiple spaces within your code, or use the TAB key or more than one tab key, and if you run without it, you can use Pycharm to run, and the error message will tell you exactly which line is wrong.

Summary of whitespace and indentation issues in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.