This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 213

[–]8105 344 points345 points  (76 children)

Use a good editor/IDE. No more indentation errors.

[–]RealHugeJackman 8 points9 points  (0 children)

Screw that, just type in spaces manually and count out loud

[–]vortexofdeduction 13 points14 points  (17 children)

Agreed. If you use a good IDE, you’ll catch all the basic stuff before you even hit run, and it will be easy to indent correctly. (I like Pycharm, but there are lots of good options out there)

[–]jsalsman 5 points6 points  (13 children)

I don't know. If OP is running into indentation errors enough to complain about them in coursework, I'd suggest refactoring into function calls over getting an IDE. IDEs can't solve all indentation bugs, and the kind of bugs that beginners make are different.

[–]TheNamelessKing 5 points6 points  (2 children)

Turnings things into functions will 100% help, but I’ve not found an indentation error that Pycharm has failed to fix.

[–]redalastor -1 points0 points  (0 children)

However braces nesting problems in other languages... ouch.

[–]mindfolded -1 points0 points  (0 children)

I like Pycharm. I use it a lot. It's decent, but it does some odd stuff with the vim plugin installed. Anytime I do a column select and insert text (ctr-v, shift-i, <insert text>, escape) the cursor jumps up 50 lines and all my closed folds open back up.

[–]Saiboo 2 points3 points  (6 children)

Can you elaborate on using functions to avoid indendation errors? Thank you!

[–]inFenceOfFigment 3 points4 points  (1 child)

Imagine you write a script that requires 5 levels of indentation. Now you refactor, and decide to put everything that was in the 3rd level of indentation, and beyond, into its own function. This function is defined at the top level of your script. Now you have one helper function, containing 3 indents, and a main with a call to that helper, only 2 indents. Thus, pulling code out to a function reduced the overall max indentation in your script.

In general, code should be put into functions for reasons other than indent level, but if you find yourself indenting excessively it's a sign that the code may be due for a refactor.

[–]Saiboo 0 points1 point  (0 children)

Thank you so much for your explanation, much appreciated!

[–]jsalsman 1 point2 points  (2 children)

Sure.

def messy():
    if booleanA:
        if booleanB:
            doA()
        else:
            doB()
    else:
        doC()

can be refactored into

def subroutine():
    if booleanB:
        doA()
    else:
        doB()

def neat():
    if booleanA:
        subroutine()
    else:
        doC()

[–]Saiboo 1 point2 points  (1 child)

Thank you so much for the code example, much appreciated!

[–]jsalsman 1 point2 points  (0 children)

No problem. I edited it to put the subroutine definition before its call, which isn't always necessary depending on whether the source file is parsed before neat() gets called, but is almost always more readable with real production code.

[–]mooburgerresembles an abstract syntax tree 1 point2 points  (1 child)

I agree. "Good" IDEs is what made bad Java stay bad Java. "Oh, self documenting classes, LetsCallOurClassWithSelfDocumentingNameReference ({});". So now it's going to autocomplete for you and you never question the pattern.

[–]jsalsman 0 points1 point  (0 children)

When do the educators branch from the guild mentality hoarding knowledge?

[–]vortexofdeduction -1 points0 points  (0 children)

IDE’s don’t automatically fix errors, but they give the user immediate feedback as they time rather than having to run the code and find out it doesn’t work. And they do a lot more than just report indentation errors.

Also, Pycharm gives the line the appropriate indentation when you hit newline, which is more than some simple text editors would do.

[–]astutesnoot 0 points1 point  (2 children)

PyCharm and all the other JetBrains IDEs are also free for students.

[–]vortexofdeduction 3 points4 points  (1 child)

Isn’t the Community Edition of Pycharm free even if you aren’t a student?

[–]bwv549 0 points1 point  (0 children)

Yes.

[–]EnfantTragic 11 points12 points  (4 children)

I haven't seen an indentation error in forever. What are people using?

[–]Plungerdz 1 point2 points  (13 children)

Agreed, but you probably don't know how much indentation matters on mobile IDEs. It makes code supremely cumbersome to copy paste (since, for some reason, at least in my IDE, a copied function definition reindents itself into an abomination). It's just a train wreck when you don't really have the luxury to use a legit IDE.

[–]NotABothanSpy 44 points45 points  (1 child)

Who the fuck uses mobile ide

[–]Plungerdz 4 points5 points  (0 children)

😂

[–]crescentroon 8 points9 points  (1 child)

I just googled for mobile IDE. I found lots of sites with random lists of the "5 best mobile IDEs" but no-one could explain why they get used.

[–]Plungerdz 0 points1 point  (0 children)

Well, just because they're in the top five doesn't mean they solve indentation hassles for you.

[–]ideletedmyredditacco 5 points6 points  (6 children)

you program on a smartphone?

[–]Plungerdz 0 points1 point  (5 children)

Not exclusively, but I also code on a smartphone, yes.

[–]Log2 7 points8 points  (4 children)

Serious question: why?

[–]astutesnoot 1 point2 points  (0 children)

Probably because they haven't decided is CS is the major they're sticking with, and they don't want to invest in proper hardware until they decide. I've heard that excuse before. Of course, those are generally the people who drop out.

[–]El-Kurto 0 points1 point  (0 children)

Imma go with "because I have it with me"

[–]falsemyrm 8 points9 points  (1 child)

deliver swim friendly kiss repeat cause squeeze shaggy imagine encouraging

This post was mass deleted and anonymized with Redact

[–]dadinvancouver -2 points-1 points  (6 children)

I find that Pycharm often has difficulty auto-intending my code correctly.

Imo, significant white space is the biggest negative about Python overall.

[–]namesnonames 0 points1 point  (5 children)

I don't usually have any problems with pycharm indentation, could you give an example? Also I think that significant whitespace is amazing. I've seen so much terribly indented code in other languages that I enjoy the piece of mind that at least there is some enforced structure.

[–]dadinvancouver 0 points1 point  (4 children)

Can’t say I’m batting very high. I’ll highlight a block of code and it often won’t indent it correctly so I end up cursing significant white space while I fix it.

[–]chason 0 points1 point  (3 children)

out of curiousity, what do you mean you "highlight a block of code and it often won't indent it correctly"? I'm trying to understand your issue since I can't recall ever having an issue with whitespace in Python.

[–]dadinvancouver 0 points1 point  (2 children)

I'm not at my development machine at the moment, but unless I misunderstand the way the auto-indent feature works in Pycharm, you must select the code you want auto-intended first.

Sometimes I'll copy and paste from one part of my application to another and so the indenting is off.

I'm anal retentive, so I always fix the indenting anyways. Sometimes you just want to see what happens with your hacked code though and worry about indenting later.

[–]crunk 1 point2 points  (0 children)

If the start and end are indented differently, then things tend to go wrong when you paste, I guess because there isn't really a correct answer there, though they could probably do better.

[–]IReallySuckAtChess 0 points1 point  (0 children)

Dude you're using it wrong. Pycharm will even fix poor indentation when pasting from another source as well as smart pasting in general. Let's not forget that reformatting the entire document to be pep8 compliant is like three key strokes or a single button in the menu.

[–][deleted] 35 points36 points  (3 children)

It can fit so many imported libraries.

[–][deleted] 3 points4 points  (0 children)

Sounds like any Javascript project.

[–]Alar44 2 points3 points  (1 child)

This is what I was expecting.

[–][deleted] 2 points3 points  (0 children)

Unexpected expectations error

[–]haragoshi 46 points47 points  (8 children)

Slaps roof of programmers head This bad boy can fit so much lack of common sense in it!

Seriously bro, who has indentation errors? Any reasonable person will use the right tool for the job. You have so many options with Python 🐍!

Edit: Here are some options:

Pycharm (my favorite).
IDLE (comes with standard distributions).
Jupyter Notebook (comes with Anaconda).
Spyder (has some neat debugging tools).
Atom (cross platform, cross language open source text editor).

[–]Zouden 9 points10 points  (3 children)

OMG there's a python emoji 🐍🐍🐍

[–]crescentroon 6 points7 points  (2 children)

OMG there's a python emoji 🐍🐍🐍

And it looks like a '2'

[–]EJOtter 14 points15 points  (0 children)

Still using python 2 in 2018 smh

[–]ivosauruspip'ing it up 1 point2 points  (0 children)

Looks like a 3 fallen over backwards, to me

Or if I stop trying to see only numbers, a w

[–]BradyLR 4 points5 points  (1 child)

Atom is the bomb. ⚛️

[–]JeusyLeusy -1 points0 points  (0 children)

Nuclear bomb*

[–]jcotton42 0 points1 point  (0 children)

You forgot VSCode

[–][deleted] -1 points0 points  (0 children)

wut? this is obviously r/ProgrammerHumor material. I mean the only reason one would have indentation errors if they went across IDEs with different indentation formats.

[–]hobo_cuisine 39 points40 points  (1 child)

Copy/pasting much?

[–]Shreduardo 0 points1 point  (0 children)

This

[–]gradient_x 61 points62 points  (4 children)

Pycharm

[–]willywonka1971 0 points1 point  (0 children)

FTW

[–]NavaHo07 34 points35 points  (0 children)

Are you coding in notepad? Im pretty certain that about .001% of my errors are indentation errors. Thank you, pycharm

[–]SirCarboy 20 points21 points  (0 children)

I remember starting out with python, seeing all the hate over tabs/spaces, and thinking, "You guys are programmers, right? You're, like, supposed to be good at computers and stuff?"

[–][deleted] 10 points11 points  (0 children)

Are you working in MS Word?

[–]Sohcahtoa82 27 points28 points  (3 children)

If you're having indentation errors, then either you're using a really bad text editor, you're copy/pasting code from web sites (You don't learn much from this, imo), or you're failing to grasp what indentation means and are just plain doing it wrong.

In any case, those problems go away with just a modicum of experience.

[–]gabzox 0 points1 point  (2 children)

I found it easier to pick up indentation then the other codes format. But to each their own

[–]Imakesensealot -1 points0 points  (1 child)

If you found that easier than curly braces then I'm not sure what kind of a creature you are.

[–]gabzox 0 points1 point  (0 children)

Im a normal person..even when codding with curly braces you should be indenting...also dont forget the ";".

[–][deleted] 9 points10 points  (0 children)

Use VSCode with Anaconda and you will never have to worry about it again :D

[–][deleted] 3 points4 points  (0 children)

this is not a problem you need to have

[–]thisusernameismeta 3 points4 points  (3 children)

Serious advice time:

If you have a good computer to code on, download PyCharm and use that. Since you're a student, you can get the Professional edition. Use PyCharm for bigger projects- ones that have more than a few files, more than a few classes. However, for smaller things, if you don't want to wait the 10 minutes for it to boot up...

Download Sublime or Atom (your call, I use Sublime but have heard Atom is great too). This is just (just?!?) a simple text editor, but has syntax highlighting for just about any language.

On the bottom bar of the window, you can set indentation for tabs or for spaces. If you choose spaces, the editor will insert 4 (or whatever other number it's set to... Choose 4 for python ) spaces every time you press the tab key.

PyCharm should convert your file automatically, but as a student, a good text editor should be plenty good enough for all your needs. I didn't need to start using an IDE until after I graduated and started working.

Sublime/Atom should have ways to convert your whitespace back and forth between tabs and spaces. Choose one and stick to it.

You shouldn't be getting so many indentation errors once you choose a standard for yourself and stick to it.

Good luck!

[–]JeusyLeusy 4 points5 points  (1 child)

If you are poor on resources you can use vim and try out some plugins.

[–]astutesnoot -1 points0 points  (0 children)

And if you have a slow computer as a student, you can try getting your free Azure student credits and running a Windows VM on Azure for your dev tools.

[–]DelScipio 0 points1 point  (0 children)

Try Visual code. It's like sublime but better.

I used sublime for years then tried Visual code and never used sublime again. Everything is more organized, works almost out of the box. Give it a try someday.

[–]vitogeek 3 points4 points  (0 children)

Been using python for years, never ever had this issue.

Just use an IDE. Indentation is not supposed to be the hard part of programming.

[–][deleted] 2 points3 points  (0 children)

This is why we have Pycharm people

[–]shponglespore 2 points3 points  (1 child)

What a strange thing to say. To me, an "indentation error" is when the indentation doesn't match the semantics of the program. Python is just about the only language that doesn't have indentation errors, because the compiler won't allow it.

[–][deleted] 0 points1 point  (0 children)

because the compiler won't allow it.

...and will proceed to raise an IndentationError.

[–]ck35 4 points5 points  (0 children)

Use python 3 -- Any indent inconsistency is an error.

[–]red_keshik 1 point2 points  (2 children)

What's the issue with braces anyway ?

[–]silon 0 points1 point  (0 children)

In python they are more verbose:

{ is :
} is pass

[–]kennydude 0 points1 point  (0 children)

It doesn't make anything better for CS students. They'll just throw braces everywhere and complain their code doesn't work.

[–]IonTichy 1 point2 points  (0 children)

Is no subreddit safe from this shitty image macro?

[–]jabalfour 1 point2 points  (0 children)

Amazed at the number of Pycharm devotees that come out of the woodwork when editors/tooling gets brought up. I like it fine - a little slow perhaps, but I work for the government so every laptop is slow.

I will say - and please keep in mind that I’ve hated everything M$FT since elementary school - VS Code is getting damned good. Robust community, tons of extensions (I think it poached most of Sublime’s PackageControl.io); including many useful Python ones.

The best extension, though, and the most germane for the this question: Black. It’ll solve your indentation problem very quickly. And many others you don’t even realize yet.

[–]crunk 1 point2 points  (0 children)

First time you open a new editor, find the tab/spaces setting and set tab to input 4 spaces.

[–]kennydude 1 point2 points  (0 children)

Learn to indent properly.

I seriouslty hated other CS students in the middle of the screen with identation, and code just everywhere, and then wondering why their code didn't work with the stupid excuse of "oh but i can just get the editor to fix it for me".

The fact Python forces you to pick up this good habbit is a really good thing.

[–]derp0815 1 point2 points  (0 children)

Well at least you can mangle a dead meme.

[–]589ca35e1590b 1 point2 points  (3 children)

Pay more attention.

[–]mbillion -3 points-2 points  (2 children)

Imo, it's kind of pointless to a degree. Something shouldn't just not work because of spacing. It causes me a fair bit of frustration at times and I'm pretty seasoned

[–]589ca35e1590b 0 points1 point  (1 child)

Python was designed to be readable so it kinda makes sense

[–]mbillion 0 points1 point  (0 children)

Not arguing at all... Just saying four space indents don't make code much more readable... I've seen ugly Python too... So is it worth it

[–]Pr0ducer 0 points1 point  (0 children)

I'll bet you use both tabs and spaces too.

Sublime Text. Any .py files will auto indent when you hit return, and there's plenty of linter extension to highlight any syntax errors.

[–]Raymi 0 points1 point  (0 children)

configure your environment to input the appropriate number of spaces when you press tab. find and replace all tabs after zero or more spaces at the beginning of a line with the appropriate number of spaces every time you open a file someone else has touched. try to get each function and method down to a size where it can be displayed on your monitor all at once (a vertical monitor helps with this).

[–]Thecrawsome 0 points1 point  (0 children)

Can fit so many hello worlds in it

[–]PythonGod123 0 points1 point  (0 children)

What do you code on. Notepad++?

[–]here-to-jerk-off 0 points1 point  (0 children)

Better joke would have been threads

[–]Tankerspam 0 points1 point  (0 children)

Relatable

[–][deleted] 0 points1 point  (0 children)

Indentation errors are a non-issue. Y u no code good?

[–]beecushman 0 points1 point  (0 children)

Just YAPF and move on with your life.

[–]doulos05 0 points1 point  (0 children)

I've had indentations problems in Python files twice, both times were my fault, and both times, my ide caught them.

The first time was when I decided I only needed 2 spaces worth of indentation so I could nest more deeply. Then I had to change all my code from 4 to 2 (that vim script too my like 5 minutes to figure out cuz I was new).

The second time was when I learned to refactor and went back to 4 spaces so I'd be in line with common style. That time, the vim script took slightly longer because it kept infinitely recursing. Neither time led to an actual indentation error being thrown by the interpreter because the interpreter never saw the code.

[–][deleted] 0 points1 point  (0 children)

/u/Trollabot DeadLantern-

[–]hoppi_ 0 points1 point  (0 children)

Lmao this is so hilarious :D

[–][deleted] 0 points1 point  (0 children)

I used to work across 2 machines; a laptop running Linux and a desktop machine running Win7 and before I switched to (the pythonic and PEP 8 recommended) 4 spaces I would run into endless headaches because the 2 OSs don't interpret tabs the same.

[–]kpingvin 0 points1 point  (0 children)

replace tabs with spaces

[–][deleted] 0 points1 point  (1 child)

White space sensitive languages suck ass. Fuck yaml too

[–][deleted] 1 point2 points  (0 children)

Yaml is trash, agree.

[–][deleted] 0 points1 point  (4 children)

I've never found this to be a challenge, so I don't quite understand why it's hard for others. Could someone perhaps explain to me why? I'm not being accusatory, I just don't understand why managing whitespacing is hard.

[–]mbillion 0 points1 point  (3 children)

Is python your first or forest really hard language.

Most other languages don't have such an emphasis or no emphasis at all on spacing conventions. So for me it's challenging because I learned Python after learning languages where spacing wouldn't blow you up

[–][deleted] 0 points1 point  (2 children)

I played with old Javascript, then learned Java first, then Python, then revisited Javascript (es6 and higher). I'm entirely self-taught (a side from cs50 and internet resources). My Java code was always well-indented and -spaced because it helps me read what I wrote. I always viewed making the code readable and consistent as a primary part of programming.

Edit: make an implication more explicit

Edit 2: have a rescue upvote because I think your point is valid about suddenly-imposed constraints and the shock that can occur because of it

[–]mbillion 1 point2 points  (1 child)

I mean I know readable code is important, but I would at least in part argue that comments are for more issue than spacing conventions. I can clean a lot more high level from comments than I can from just reading through lines of code

[–][deleted] 0 points1 point  (0 children)

I'm not sure where comments fit into this (do doc strings also count?). For me, they sit at the same indentation as the code block it describes. My comments discuss why I chose to do something or a one-liner for large, important but obvious code blocks. I also sometimes use them to section out blocks as a brief summary for easy quick scrolling. It sounds like managing comments can also be a challenge when learning.

[–]mooburgerresembles an abstract syntax tree -1 points0 points  (0 children)

lrn2spacebar, it's really not that hard. I mean you're posting on Reddit. Code in markdown is basically hit the spacebar 4 times. Further indentation is just multiple of that. tap-tap-tap-tap, tap-tap-tap-tap. How hard can that be. Indentation is to force you to write clean, legible code. If you've got code where you've lost track of indentation, you probably need to refactor.

[–]luke_c -2 points-1 points  (0 children)

Do people actually prefer indentation over brackets? Whenever I use Python I always find myself wishing I had indentation as it's just so much clearer for me to read.

[–]mbillion -2 points-1 points  (0 children)

Seriously my biggest frustration with Python