Python 2.7 Indentation Overview

I am trying to get LOT of IndentationErrors when writing Python code. Sometimes the error is deleted when I delete and rewrite the string. Can someone provide an explanation of the indentationErrors level in python for noob?

Here is an example of a recent indentationError that I received while playing CheckIO that won't go away:

def checkpass(data): """Checks password for >=10 char + 1 number + 1 LC letter + 1 UC letter""" passlist = [] uclist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'] lclist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] blah blah.. more code 

Here is the error I get when I call python script:

 $ python passcheck.py File "passcheck.py", line 5 uclist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', ^ IndentationError: unindent does not match any outer indentation level 
+5
source share
1 answer

Such problems usually occur when mixing tabs and spaces.

Some editors may display Tab as 4 spaces, but in Python 2.x Tab is interpreted as 8 spaces, so although the editor shows that multiple lines are indented at the same level, they may not really be indented at the same level for python (especially if the editor interprets the tab as 4 spaces)

In Python 3.x, you are not allowed to mix tabs and spaces in general, this will result in a syntax error for mixing tabs and spaces in the same python script.

Ideally, you should use full tabs or full spaces throughout the script, 4 spaces are the recommended practice for indentation.

+6
source

All Articles