This indent thing is whacked


#1


what are the rules for indention, sometimes not having the indented per python i get a syntax error?
how can else: have a syntax error? i spend most of my time on the indent function, i don't get it.

thanks

jack g

correct practice makes for a better understanding of the subject matter, incorrect practice doesn't make sense,

practice for the sake of practice is not IMO going to get towards a mastery of the subject, my 4.67 cents worth.

thanks for your time...


Replace this line with your code.


#2

The indentation is how the code recognized the flow of processes. Check out PEP8, a thorough understanding of the language may help to alleviate some frustration


#3

Thanks Alan for your time, i'll read the information,

have a great week..

jack g


#4

Yeah, sometimes I have issues with indentation as well. Alan linked a cool guide, but I'll try to share my tips as well.

  • Basically we always indent things in functions that are parts of the function. Think of it as grouping everything related to this function in that function. For example if you have something like that:

def function(arg):
    if something:
      thing

    return thing

Obviously this is not a well-written code, it's just an example. Basically the indent after defining the function allows you to tell the computer that everything that is indented is a part of this function. Now, why is that important? Well, if you put that return command without the indentation, the computer is gonna think that you want to return a thing from outside the function. Now, this sometimes makes sense if the code is bigger and there are more than one function, and sometimes you want to return things outside of the functions.

  • Now, you want to do indentation thing inside the function as well. You basically always indent after giving an if command, because it tells the computer that something happens specifically after this very specific condition is fulfilled. It also allows you to put multiple ifs and still know, which conditions are tied to which results, like that:

if something:
    something
elif something_else:
    something_happens_now
elif ...

Now, as you see, indentation allows you to see instantly what is the structure of given code. So yeah, it is worth to learn how to do it, because it will make your work easier so much.

  • Now, a lot of my troubles concerning indentation come from the fact that sometimes the code gets a bit messed up in the editor and sometimes in one place you have four spaces and in second place you indent with tab key. Different editors interprete it in different ways so you need to be really consistent with that.

  • Overall Python is a lot about being consistent. It is hard, yeah, but once you get used to that, it's getting pretty easy and if you move to another programming language, you'll appreciate how easy-looking is Python code :wink:


#5

thanks Karaeska,

your explanation really helps me,

I appreciate your time, the indents are just crazy..

thanks again

jack g


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.