How do I properly indent my code in python?


#1

The most common error I get is some sort of indent error. I try figring it out but the code seems correct to me. Eventually I check the solution, and the only diference is 2 vs 4 spaces indent. Thinking that 1 tab = 2 spaces, I try re-writing the code with the same indent amount. Still won’t work. Then I try using for spaces for each indent, and it “magically” woks.

What is the diffrence between spaces and tabs?

Should I use 1 tab (2 spaces) or 2 tabs (4 spaces)?

Do i have to use spaces over tabs?


#2

You can use any number of spaces, as long it is consistent. The industry norm seems to lean toward four spaces per block level.

Tabs add a lot of whitespace which may affect readability. The issue that arises out of this is mixing of tabs and spaces. Python does not dictate what the author uses, but will kick up a fuss when tabs and spaces are mixed.


#3

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