Notepad++ spaces not indenting correctly in Python

Tensigh
2014-01-20
2014-01-24
  • Tensigh
    Tensigh
    2014-01-20

    Hello,

    I'll often write code in Notepad++ then paste it into a python interpreter.
    Even though the code is correctly indented in Notepad++, Python cries foul
    and I have to find the offending lines and re-indent them using space in the
    Python interpreter. What am I doing wrong?

    Any help would be greatly apprecitated.

     
    Attachments
  • Neomi
    Neomi
    2014-01-24

    Seems like you have mixed tabs and spaces. If you didn't change the settings, you have a tab width of 4 in Notepad++, but your Python interpreter doesn't know that. The interpreter may be assuming a tab width of f.e. 8. In that case, the indentation "changes" and with it your program flow. If you copy code indented with spaces and edit it, that is most likely to happen. If you press enter, Notepad++ not only inserts a line break, but also an indentation (depending on settings again). This indentation however is no simple copy of the indentation of the previous line, instead it is constructed using tabs with the default settings.

    In "Edit -> Blank Operations" you can easily convert tabs to spaces (or spaces to tabs if you prefer that), you don't have to do that manually. In "Settings -> Preferences -> Tab Settings" you can control your tab width and if spaces should be used instead for each language separately if you want to, use it to your advantage. Then I would recommend to enable the display of white space (in "View -> Show Symbol") and set its color (in "Settings -> Style Configurator") to something with a low contrast to the background so that it can be easily seen but doesn't distract from the rest.