+ 2
How does one know how to correctly indent a code in Python? I'm new to Python and not very proficient at it.
5 Réponses
+ 4
Indentation indicates a relationship between the indented lines and the 'parent' line. Look at it as an immediately local context / like a closed book; it has content but it may not be relevant to what you're doing right now.
Take the 'if' statement. You only want to do something if a test is true, or if it isn't true...like if the book is a romance novel you read it, otherwise you put it away. Each time a test can pass/fail, you 'set aside' (indent) code that runs only in that case.
Similarly, code indented in a loop only applies to that loop; the last indented line is the 'bottom' of the loop.
while DirtyDishes == True:
list
of
tasks
if NoDishesLeft:
DirtyDishes=False
If "DirtyDishes=False" was not indented under the testing 'if', you'd do one dish before stopping (and it would be a logical 'bug', i.e., bad logic).
+ 3
U need reed this:
https://www.python.org/dev/peps/pep-0008/
+ 3
Thank you Alex, great resource. Also Kirk, wonderfully explained, thank you.
+ 2
Thanks
+ 2
Thanks Kirk. Great help