How to debug indentation errors in python

I am trying to write my very first python script. This was working but then after some slight refactoring I have, apparently, broken the indentation. I can not determine what is the problem. The interpretor complains about the following method. Can someone point it out?

def dataReceived(self, data):
    a = data.split(':')
    print a
    if len(a) > 1:
        command = a[0]
        content = a[1]

        msg = ""
        if command == "iam":
            self.name = content
            msg = self.name + " has joined"

        elif command == "msg":
            msg = self.name + ": " + content
            print msg

The error reads: File "python_server.py", line 17 a = data.split(':') ^ IndentationError: expected an indented block

Answers


You start using a text editor that allows you to show indents, and you become consistent about using spaces instead of tabs, and you enforce that in your editor.


I encountered a similar problem using Sublime Text 2.

To solve, click on the "Tab Size" at the bottom of the editor, and choose "Convert Indentation to Tabs".


There are a great number of things you can do here:

  1. Use an editor that can show control characters (like vi with set list).
  2. Use a hex dumper program like od -xcb.
  3. Just delete the white space at the start of that line and re-insert it (may want to check the preceding line as well).

Try Editra - www.editra.org

Your code looks fine, syntax seems fine...your text editor may be creating your errors. Review your file with Editra to see/review indentation levels.

Editra saved my sanity - I thought I had correct syntax when viewing my script in Text Editors including Notepad++ with python indent plugin. However, when I would run the script, it would throw off indentation errors every time. I finally opened the script up in Editra, and I could see the problem. Notepad++ and other text editors did not show correct indentations/tabs/spaces. Editra showed errors e.g. unexpected spaces, tabs - which I was able to correct.

Editra will auto-indent your script [and show errors -tabs, spaces -that may not show up in other text editors].

If you have indent errors it will show up as blue underlined segment;

If you are writing script [adding/deleting lines] Editra will auto-indent the script.

**I would suggest opening your script and editing it in Editra.

Hope this helps! Best of luck. str8arrow


if you're using the "Sublime Text 2" editor, then I found this answer helpful - it details how to turn on whitespace characters and also convert tabs to whitespaces

sublime-text-2-view-whitespace-characters


Need Your Help

Cannot access Amazon SQS message attributes in C#

c# amazon-web-services amazon-sqs

I have a process that creates SQS messages and places them on an SQS queue and another process that reads those messages and performs certain logic based on the contents of the body and attributes ...

Changing master volume level

c# winapi volume

How can I change the master volume level? Using this code