Inconsistent use of tabs and spaces in indentationThe 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.
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:
Examples of inconsistent use of tabs and whitespacesNow 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. 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. 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. 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. Example 4: The output of the above program: Due to whitespaces, the output cannot be generated even though the program is correct. 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. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263316.html