Indendation problems in python

Indentation is a part of code structure in python.

Indentation problems cause error messages like

IndentationError: unindent does not match any outer indentation level
IndentationError: expected an indented block after ‘for’ statement on line 3


There are many, really many tutorials in the web about this topic.
https://python.land/python-tutorial#Pyt … r_features
https://peps.python.org/pep-0008/#indentation
https://www.w3schools.com/python/gloss_ … tation.asp

Usual problems are:
– various deep levels, e.g. 3 blank or 4 blank deep mixed (use 4 blanks)
– mix of blanks and tabs for indentation.

Thonny is a common IDE, editor for beginners. See https://thonny.org/
Too see the difference between tabs and blanks, it is good to have ‘highlight tab characters’ enabled in the options.

thonny_options.PNG

Then in the thonny editor, tabs are displayed in a special color. Usually a good idea to replace tabs by blanks.

thonny_tab.PNG

The sample shows two tab in second line.

There are text editor around which allow better visibility of all these invisible characters in a text. Here an example of a blank followed by a tab in second line.

editor_show_all_characters.PNG

This is from “Notepad++”; good to have such a tool in the toolbox to fix stubborn problems.