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

User97693321
  • 3,336
  • 7
  • 45
  • 69
Nick
  • 19,198
  • 51
  • 185
  • 312

5 Answers5

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

samwize
  • 25,675
  • 15
  • 141
  • 186
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.

dkamins
  • 21,450
  • 7
  • 55
  • 59
  • 2
    Yeah.. you're right. I opened the file in another editor and the indentation was different. Sublime Text was tricking me. Thanks. – Nick Apr 05 '12 at 02:53
  • 2
    5 years later and Sublime Text still hasn't fixed this issue. – Mukherjee Dec 27 '17 at 07:27
  • 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` – rosinghal Apr 21 '18 at 11:15
  • 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. – dkamins Jan 27 '21 at 02:30
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).
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
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

robbie70
  • 1,515
  • 4
  • 21
  • 30
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

str8arrow
  • 111
  • 1
  • 3