[Solved] Indentation error in python, how do I check for spaces or tabs to avoid the error? [duplicate]


From the documentation:

Indentation is rejected as inconsistent if a source file mixes tabs and spaces in a way that makes the meaning dependent on the worth of a tab in spaces; a TabError is raised in that case.

You state the body of the with statement is indented with 3 tabs. If that is true, it would appear the line with with itself is indented with 4 spaces. That means that if tab stops were set to just a single space each, the body of the with statement would no longer be indented relative to the first line, resulting in the TabError.

Consider this code (with tabs replaced by a $):

for y in [1]:
    for x in [1,2,3]:
        if x == 2:
            print("even")
$else:
$    print("odd")

If your tab stop was set to 8 characters, this would look like a for loop that contains an if/else statement. If the tab stop was set to 4 characters instead, it would look like a for loop with an else clause. Other tab stops would look like invalid uses of indentation.

Python 2, by contrast, would replace tabs with spaces during parsing so that the indentation was a multiple of 8, and only then determine if the resulting indentation was consistent. This could lead to unintended indentation errors, as code could still parse but have a behavior different from its “visible” indentation. For example, the preceding example would be accepted by Python 2 as in if statement with an else clause, even though in an editor using tab stops of 4 spaces it would look like the else went with the for.

solved Indentation error in python, how do I check for spaces or tabs to avoid the error? [duplicate]