Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 769
Nick Avatar asked Apr 05 '12 02:04

Nick


People also ask

How do you manage indentations in Python?

To indicate a block of code in Python, you must indent each line of the block by the same whitespace. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to.

Why am I getting indentation errors in Python?

The cause of Indentation Error in Python Since python makes use of procedural language, if you miss out on adding tabs or spaces between your lines of code, then you will most likely experience this error.

How do I fix my indentation?

Go to Home and select Line and Paragraph Spacing > Line Spacing Options at the bottom of the menu. The Paragraph dialog box opens. On the Indents and Spacing tab, select the options you want, and click OK. The Paragraph dialog box options are described in Adjust indents and spacing.


1 Answers

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".

like image 170
samwize Avatar answered Sep 19 '22 05:09

samwize