Inconsistent use of tabs and spaces in indentation

The word indentation here refers to the number of spaces and tabs while beginning any statement or block of code. The use of these whitespaces and tabs must match while a certain code block ends; thus, it refers to the proper indentation of a particular block.

  • The indentation is used in the python programming language and plays a very important role.
  • To initialize any block of code or begin a certain block of code, the indentation is used to identify that block of code.
  • In other programming languages like C, C++, Java, C#, and many more, the indentation does not matter for starting any code block.
  • Instead of indentation, these languages use curly brackets to denote a code block.
  • But in the python programming language, indentation matters. The whole code and its running process start only with the matching indented block.
  • In the python programming language, we did not have the concept of the main function when compared with other programming languages, like C, C++, C#, Java, and so on.
  • The initial block of code does not contain any white spaces and tabs, ultimately denoting the main function; after onwards, as per the requirement of the programmer. They will use the if-else, for loop and many more the indentation vary accordingly.
  • As the name suggests, inconsistent use of spaces and tabs in indentation generates a syntax error.
  • When we code in any particular programming language, we need to remember the syntax accordingly; hence, we did not apply the unnecessary white spaces.
  • If we do unnecessary use of whitespace and tabs, it will generate particular errors accordingly.
  • If we talk about the python programming language, it is more concerned about the proper indentation and right use of whitespaces and tabs for accessing code.
  • If we talk about the C programming language, it is more concerned about the proper use of brackets at the particular place wherever required for starting and closing certain blocks of code.
  • But suppose we do unnecessary use of whitespaces and tabs in C language. In that case, it is ignored by the compiler because it is predefined in the working of the c compiler, that it will ignore all the whitespaces and comments while executing the program.

How does indentation work in Python?

The indentation term here refers to matching the number of tabs and whitespaces used before starting any block of code and the end of a certain block of code because it ultimately represents a block of code. Similarly, in this way, we can do the use nested code blocks, but the indentation of that block of codes refer from block to block.

If we talk about starting block, there is no use of any whitespace and tab, but when we do move further for the upcoming second block of code, we use the 2 whitespaces. While starting the certain block of code, for its ending, we will do the same and apply the 2 whitespaces, which indicates the ending of that block of code.

If we want to include more nested blocks of code, we will apply more whitespaces than the previous block of code, which indicates the starting of the next nested block of code. We will apply the same number of whitespaces as we have done at starting that block of code for its ending.

Similarly, in this way, we can add more nested blocks, and the starting and end of each block will differ in the use of the number of whitespaces used to represent a particular block of code. But as we use the nested block of codes while going nested inside, the number of whitespaces and tabs has increased.

In the Python programming language, the indentation of each block of code plays a very important role in indicating a particular code block.

What happens when we do inconsistent use of whitespaces and tabs?

As we talk about the python programming language, when we inconsistently apply the number of whitespaces even at unnecessary places that were not required, it will ultimately generate an indentation error.

The indentation error will occur when we start the block of code properly, but while ending it, we will use the irregular use of several whitespaces and tabs for indicating that particular block of code; hence it will generate an error due to an unmatched number of whitespaces and tabs.

If we talk about the C programming language, the number of whitespaces and tabs is immaterial; it does not generate any error because the C compiler ignores it. But sometimes, it may generate unusual things while running the ' scanf ' function and applying the extra whitespace; we will understand it by discussing it in more detail.

How to rectify the inconsistency in code due to unnecessary use of whitespaces and tabs?

To rectify the error, we have to follow the steps that are mentioned below:

  1. Identify the line of code that is generating the error.
  2. Observe the line and find its corresponding block of code.
  3. Match the number of whitespaces used for starting that block of code by counting the whitespaces and tabs.
  4. Count the number of whitespaces used to end the particular code block; if it does not match, then rectify it by applying the proper number of whitespaces and tabs.
  5. If still error not resolved, then match the indentation of the target block of code with the above and below block of code. For matching it, similarly as above check the number of whitespaces used for starting each block of code. It must contain the difference of 2 whitespaces. If these are nested else by randomly matching the number of whitespaces rectifies the corresponding indentation error.

Examples of inconsistent use of tabs and whitespaces

Now let us understand it in more detail with the help of examples:

Example 1:

The output of the above program:

The above-highlighted block will generate the indentation error due to an unmatched number of whitespaces and tabs while starting that code block with its ending. Even a nested indentation will affect the above block of code.

IndentationError: expected an indented block  

Correction of example 1:

The output of the above program:

The above program will perfectly work as if the error has been rectified by matching the number of whitespaces and tabs used to represent a certain code block.

Inconsistent use of tabs and spaces in indentation

Example 2:

The output of the above program:

The above-highlighted block will generate the indentation error due to an unmatched number of whitespaces and tabs while starting that code block with its ending. Even a nested indentation will affect the above block of code.

IndentationError: expected an indented block    

Correction of example 2:

The output of the above program:

The above program will perfectly work as if the error has been rectified by matching the number of whitespaces and tabs used to represent a certain code block.

Inconsistent use of tabs and spaces in indentation

Example 3:

The output of the above program:

The output of the above program can be generated correctly. Although it will not generate any error, we will not get the desired output if we use unnecessary whitespaces. One extra element in scanf function will disturb the working of the above program. Thus, it is known as the inconsistent use of tabs and spaces in the above program.

Inconsistent use of tabs and spaces in indentation

Correction of example 3:

The output of the above program:

Here the output of the above program is perfect, just by removing the unwanted whitespaces in the scanf function.

Inconsistent use of tabs and spaces in indentation

Example 4:

The output of the above program:

Due to whitespaces, the output cannot be generated even though the program is correct.

Inconsistent use of tabs and spaces in indentation

The correction of the above code is as follows:

Correction of example 4:

The output of the above program:

Here the output of the above program is perfect, just by removing the unwanted whitespaces in the scanf function.

Inconsistent use of tabs and spaces in indentation




Latest Courses