+ 6
Having issues with indentation
I'm not understanding how the whole indentation of code works. How much should you indent? Thank you.
2 Answers
+ 5
Thank you Philip. I will remember four spaces. Thank you so much for your answer to my question.
+ 2
The easiest rule for using Python indentation with if statement is to use indentation after each line with column.
For example:
if x==1:
print(1)
elif x==2:
print(2)
else:
print('No result.')
By indentation I mean four spaces. Please note that most of the editors will translate a tab to four spaces. If your editor does not translate tab to spaces the code may and may not work. Moreover, the tabs are set differently in different systems/environments so if your code includes not transformed tabs it may cause confusion/unusability of the code.
Adding additional levels to your code works exactly the same as with the first level. Example:
if x in range (5):
if x < 2:
print('lower')
else:
print('upper')
else:
print('x is greater than 4.')