Net-informations.com

What is unexpected indent in Python?

As the error message indicates, you have an indentation error . This error occurs when a statement is unnecessarily indented or its indentation does not match the indentation of former statements in the same block. Python not only insists on indentation, it insists on consistent indentation . You are free to choose the number of spaces of indentation to use, but you then need to stick with it. If you indent one line by 4 spaces, but then indent the next by 2 (or 5, or 10, or ...), you'll get this error. Whenever you have a situation with code inside of a statement, that inside code must be indented, and must be indented consistently. It is used by the interpreter to know how to delimit blocks of instructions.

How to indent my code?

The basic rule for indenting Python coding style is: The first statement in a basic block, and each subsequent statement after it must be indented by the same amount. For example, the second line in the program below is unnecessarily indented:

output


Javascript String length

In order to fix this error is to first make sure the problematic line even needs to be indented. For example, the above example using num = numbers[7] can be fixed simply be unindenting the line:

However, if you are sure the line does need to be indented, the indentation needs to match that of a former statement in the same block . Run your code with the -tt option to find out if you are using tabs and spaces inconsistently. The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.

Python IndentationError and tabs

Spaces are the preferred indentation method. But, Python realizes that some people still prefer tabs over spaces and that legacy code may use tabs rather than spaces, so it allows the use of tabs as indentation. However, by default, mixing tabs and spaces is still allowed in Python 2 , but it is highly recommended not to use this "feature". Python 3 disallows mixing the use of tabs and spaces for indentation. Replacing tabs with 4 spaces is the recommended approach for writing Python code .


Python IndentationError and tabs

Python Indentation

Most programming languages permit indentation , but don't enforce it. Python enforces it with an iron fist. This is different from many other programming languages that use curly braces {} to delimit blocks such as C, C++, Java and Javascript. Because of this, Python users must pay close attention to when and how they indent their code because whitespace matters. Python's use of indentation comes directly from ABC . ABC is an interactive programming language and environment for personal computing, originally intended as a good replacement for BASIC .










net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About