6
chakram
3y

Where do I even start???? Python is killing me. I am a beginner when it comes to coding but somehow the indentation in Python gets me every time. No matter how many times I follow a tutorial I always end up with at least one indentation error. How do people do this?!?

Comments
  • 2
    This is slander
  • 6
    1) your IDE solve the indentation problem for you

    2) always use "pass" keyword for thing you are going to add later (just to make python happy)
  • 1
    you generaly use editor which by taping TAB creates correct offset in spaces. So you can easily indent code.

    also correct new line if you press enter on end of line.
  • 0
    Please be consistent, all space or all tab
  • 0
    Once you get it it's simple and always follows the same logic: everything that should be included in a code block has a deeper indentation, at least one code line with the same or lower indentation ends that block/scope.

    If all else fails you could do a quick tour of a different programming language until you get an intuition for scopes, and then you just gotta remember that in Python +1 indentation is opening a scope and -1 indent is closing one.

    Or just play with indentation in Python and try all kinds of things until you know how it behaves. That's probably the fastest and least frustrating approach. Trying to learn it as you go can be cumbersome.
  • 0
    What the other said ^ this is one of the biggest reasons I don't recommend python for beginners
  • 3
    Imagine using Tab as a fucking indent.
  • 1
    I've worked with a small to medium-ish sized code base in python before and yes it's messy

    I always ended up thinking "oh in what function am I in right now", I can't stand indentation based syntax. Curly brackets ftw
  • 2
    wait so you're telling me python is hard, because you need to write properly structured code?

    @ScriptCoded
    That's... the first time, i hear that. That language is quick to pick up, and i cant wrap my head around, how it can be hard for beginners. You need to properly indent anyways, to not get the hatred of your coworkers, when working in a project later; so why not start with that directly?

    Almost any editor now can handle indentation correctly, and if not, you can still resort to spaces all the way, if you're not sure.

    granted, python wasn't my first language either, but i learned the syntax in two afternoons, so not sure, what the problem is..
  • 0
    @Python *squints* you're not the guy I'm talking about, but you seem nice :)
  • 0
    > I always end up with at least one indentation error

    Seems not bad. If it is one indentation error, then you can fix it in five seconds 🤔 Just select the block of code and press Tab or Shift+Tab, then continue learning
  • 0
    @Ranchonyx Imagine thinking Python can't handle space-based indentation.

    It doesn't accept a mix though, which is fair.
  • 0
    @Ranchonyx Why wouldn't you?
  • 0
    @sudo-woodo IN MY BRAIN TAB AKA THE HOLY "\t" IS RESERVED AS A STYLISTIC DEVICE IN CODE AND NOT A GODDAMN INDENT.
  • 0
    @Ranchonyx but tabs > spaces

    ...

    oh god i've re-started the holy war
  • 0
    @sudo-woodo I HEAR HOLY WAR?!

    For the grace, for the might of our Lord
    For the home of the Holy
    For the faith, for the way of the sword
    Gave their lives so boldly
    For the grace, for the might of our Lord
    In the name of His glory
  • 0
  • 1
    @Ranchonyx Repeat after me. Tabs are for indentation. Spaces are for alignment. Tabs are not for alignment. Spaces are not for indentation. Preach.
  • 0
    Most have to have a dedicated python ide or a python plugin to help manage any sizable file.
  • 1
    @ahuggins0006 Not really.

    I code in nano a lot of times when I need something done quickly on remote server.

    autopep8 -i script. Be done.

    As pip can install in a home directory it's more a question of how much you want to punish yourself or just let the tools do the mundane work for ya.
  • 0
    tbf i only use the tab key to indent stuff in python, but it usually is not translated into the tab key, it is rather a macro to create some amount of spaces to align the blocks of code.
    I think jetbrains does it like that, and other Tools do that aswell.

    And i'm honest with y'all, i wouldn't want it any other way.

    I don't care about tabs as characters outside of csv files.
  • 1
    @thebiochemic I'd argue that a language that doesn't rely so heavily on indentation and that uses brackets or something like that for blocks makes it a lot clearer for a beginner. Sure, an editor can fix indentation for you, but with Python you actually have to spot a lot of spacing mistakes yourself as the indentation changes the behavior of the program. For some reason it also seems that people I've taught programming grasps the concept of scope a lot easier with programming languages with clearer blocks.

    And for the point of arguing, just because you can learn a syntax in two days doesn't mean that a beginner who barely know what programming is can.
  • 1
    Let me introduce you to the infamous Apple SLL bug which was caused by the unclear use of curly braces https://blog.codecentric.de/en/...
  • 1
    @ScriptCoded i agree that beginners will likely need more time to learn python as a first language.

    but i'd argue, there is a difference between understanding a feature and using it correctly. Beginners tend to use stuff, they just learned incorrectly.
    In C family languages for example you don't need any indentation to structure logic into blocks, only curly brackets. And the time i was helping out one of the Professors at University, that showed. A lot. Some students had extremely hard to read code, that by pure syntax was not that complicated. It's readability tanked, because they ignored the guidelines for clean code with the excuse to do it later.

    And for exactly this reason i would prefer teaching someone python first and C later for the sole purpose, that they understand, what indentation is "actually" for.

    If they know, that blocks have to be visually distinct, it doesn't matter if the block later is encased in curly brackets or begin..end or whatever else there's out there.
  • 1
    @thebiochemic If "fixing it later" is the reason that someone doesn't understand something then the issue isn't the language, but the way the learner tackles the task, or rather how they're taught and guided.
  • 1
    @ScriptCoded while that's true, the language can atleast counteract that and manifest correct use.

    Think rust for example. You will have to think about the lifetimes of variables. that is talked about in theory, but never quite used. But in the rust case the compiler explodes, if you don't care about that, so you basically have to learn it one way or another. If you have no experience whatsoever, that's okay. Python is similar. When talked about style guidenlines, you will not care at first, but in python find out, that indentation is important and you basically have to learn that one way or another. But when learned, you can use it correctly everywhere (or atleast almost, since python has sometimes weird indentation aswell).
  • 1
    @thebiochemic I see your point, but I'm not sure I agree. Let's disagree.
  • 1
Add Comment