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.

5
  • 3
    Are you sure you're not mixing tabs and spaces? Try undoing and redoing the indents Commented Oct 8, 2012 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? Commented Oct 8, 2012 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. Commented Oct 8, 2012 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! Commented Oct 9, 2012 at 5:18
  • "I'm using Notepad++ and there are no spaces/tabs issues anywhere" - the "extra tree-expander" is, in effect, Notepad++ telling you that there is such an issue. Commented Dec 23, 2023 at 9:04

1 Answer 1

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:

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

4 Comments

@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.
@MartijnPieters *no one
@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..
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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.