16

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

1
  • It's not clear to me whether the question is supposed to be about knowing what indentation to use, or about using an editor to create that indentation. But either of those has a better canonical duplicate. Commented Dec 25, 2023 at 2:35

5 Answers 5

53

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

Sign up to request clarification or add additional context in comments.

Comments

5

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.

4 Comments

Yeah.. you're right. I opened the file in another editor and the indentation was different. Sublime Text was tricking me. Thanks.
5 years later and Sublime Text still hasn't fixed this issue.
I use this settings in Sublime. Helps a lot. "translate_tabs_to_spaces": true, "ensure_newline_at_eof_on_save": true, "trim_trailing_white_space_on_save": true
In Sublime Text you can select all (CMD-A on Mac), and you can see the difference in indentation visually: the tabs will show as lines, and the spaces as dots. This is a quick way to identify any obvious issues.
3

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

Comments

1

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

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.