1

I'm new to Python and it just confounds me with Indentation errors. In the following images, why does the first one work and the second one give me an indentation error?

Works:

This works

Doesn't work: (Notice extra tree-expander that pops up in Notepad++)

enter image description here

Error:

  File ".\sigma.py", line 14
    for val in vs:
    ^
IndentationError: unexpected indent

I'm using Notepad++ and there are no spaces/tabs issues anywhere. Also, tried it out on the Python console typing it in exactly the same way in the 2nd image. It works fine. I'm guessing there's a very logical explanation to this, but coming from a strong-typed background (>5 years in Java), this feels like an unnecessary error.

Plasty Grove
  • 2,807
  • 5
  • 31
  • 42
  • 3
    Are you sure you're not mixing tabs and spaces? Try undoing and redoing the indents – inspectorG4dget Oct 08 '12 at 17:35
  • I use NPP for python programming all the time but I've never seen an errant `[-]` nor had problems like you describe. What is your tab setting? – TheZ Oct 08 '12 at 17:35
  • 3
    The only way I was able to reproduce that in Notepad++ was using mixed indents (tabs and spaces). I'm pretty sure this is the issue here. And it's not an "unnecessary" error, because indentation is very much part of the syntax in Python. – NullUserException Oct 08 '12 at 17:36
  • Yup, the tabs and spaces issue is correct. I was under the impression that I had it enabled in Notepad++ but it wasn't. Thanks! – Plasty Grove Oct 09 '12 at 05:18

1 Answers1

6

You are mixing tabs and spaces. Don't do this, it creates inconsistent indentation problems.

Run your script through the tab checker:

python -tt script.py

and fix any and all tabs (replace with spaces), then configure your editor to only use spaces.

For Notepad++, see:

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • @Cfreak: There is a *reason* the styleguide specifies to use spaces only. Noone can ever agree on the visual size of tabs, they are not visible enough, and the 'they save space' argument doesn't hold anymore with todays storage capacities. Most of all, they lead to problems like the OP just experienced. – Martijn Pieters Oct 08 '12 at 17:40
  • @MartijnPieters [*no one](http://english.stackexchange.com/questions/8741/noone-no-one-or-no-one) – NullUserException Oct 08 '12 at 18:40
  • @NullUserException: I claim foreign-ness. :-P English is not my first language, so if I speak it loud and slowly, would that help? :-) I will, at times, get grammar and spelling mixed up, especially since I no longer live in my native country either, and the local language is yet another Germanic one with plenty of subtle traps for me to fall into when speaking or writing in a foreign tongue that is vaguely related.. – Martijn Pieters Oct 08 '12 at 19:50
  • I was fairly certain I had the tabs to spaces enabled in notepad++. Turns out it was in my work laptop and not my personal one. That fixed the issue, thanks! – Plasty Grove Oct 09 '12 at 05:17