Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 101: Indentation Errors
Python is a fickle language to write with and indentations MUST be used correctly. Whilst this can be a real PITA (I'm constantly getting indentation errors in my log when coding) it does mean the code should always be tidy and easy for others to follow. This makes collaborating or forking of code much easier and also makes it far easier for the original author to bug-track too.


I'm going to presume you're using Notepad++ as that's what I use, there are other good options out there but Notepad++ is my personal favourite (EDIT: Actually I've switched to sublime which is much better). The use of a decent text editor designed for coding is a MUST. Using a text editor made for Python coding will save you lots of wasted hours and frustration as it colour codes everything which makes it much easier to find mistakes in real time whilst you're typing the code.

Take a look at the following snippet of code (just a random piece of my code I've done a screenshot of):

[Image: 2k0znm.jpg]

You can see the statements such as if/try/except/True/False are in blue - Notepad++ does this automatically as it recognises they are actually Python statements. Another useful thing the program does is it will add vertical lines at every indent so you can see where your next line of code should be starting.

Where should I indent?
Wherever you have a statement telling the code to do something (like the "if" statement on line 486) the next line should be indented, the standard is 4 spaces but not all coders adhere to these standards - so long as it's indented with spaces the amount of spaces is irrelevant.

Everything in the indented section has to line up, if you accidentally add an extra space Python will error. In the example above we have an "if" statement followed by a "try" statement so there are 2 indents. As you can see by the useful vertical lines everything from line 486 to 492 is related to that "if" statement. Basically what the above code is doing on line 486 is checking to see if a specific path exists on the system, if it does then it will perform everything in that block (down to line 492). You'll see line 493 is in line with the "if" statement so the code will continue as normal. In other words if that "if" statement came back as False it would skip that indented code and go straight to the next bit of code in line with the "if" line which happens to be line 493.

Be Careful Of Auto-Indent
Notepad++ will auto indent for you, so when you press enter after a statement it will automatically indent the next line. This is great but by default the program uses tabs rather than spaces and when you look back in your log to see what's going on you'll see indentation errors. Luckily the fix is very simple, what you want to do is click on Edit>Blank Operations>Tab to space. This will then change all tabs to spaces, don't forget to save your document and then you should have no more indentation errors. This has to be done every time you save the document unless you change settings so that spaces are always used instead of tabs.

[Image: 2501h0l.jpg]
If anyone wants to get into coding in python... I can highly recommend this piece of software:

One feature of the software is you can get it to check all your indentations and fix them.

This is particularly useful if copying and pasting code from other files or online places like
Top tips Whufclee, and Dixie Dean
[Image: 34td1f8.jpg]

Possibly Related Threads...
Thread Author Replies Views Last Post
  how do I execute install from zip in python nickhere 2 60 01-15-2018, 02:11 PM
Last Post: nickhere
  Python Question Android Kodi Based_Skid 2 155 08-08-2017, 02:27 PM
Last Post: Based_Skid
  How to get mac address of device via python bluedangerforyou 2 556 12-09-2016, 04:33 AM
Last Post: bluedangerforyou
  Python, how to launch addon inside of a subfolder bluedangerforyou 5 1,045 12-06-2016, 07:47 AM
Last Post: bluedangerforyou
  Python script outcome OS dependent? OptimusGREEN 9 1,552 11-05-2016, 05:14 AM
Last Post: ][NT3L][G3NC][
  kodi python help needed, opty 4 1,111 08-11-2016, 02:52 AM
Last Post: opty
  Python Code to change to default skin. kodiuser 7 1,182 06-28-2016, 05:15 AM
Last Post: kodiuser
  Help with creating python plugin Pinata 4 1,392 04-07-2015, 04:57 PM
Last Post: whufclee

Forum Jump:

Users browsing this thread: 1 Guest(s)