Python is really nice, easy and addicting. It allows to sometimes write code really quick and error free as you don’t have to type semi colons ( ; ) after each line and use brackets. By indenting your code properly, you don’t need all that.  However, the downside to it are those indentation errors which occur quite frquently if you are a beginner. Here are some tips to ban those errors definitely. Yes, it’s easy. Stop all those:

Unexpected indent at line 28

 

Expected indent at line 73

 

line 19 : Indentation does not match any other indentation level

Rule 1 : Make sure indentations correspond 

def function():

    print ‘something’

Easy but first make sure to see if you put the required indentation. E.g. don’t put the print at the same indentation level like the def like:

def function():
print ‘something’

Rule 2 : Don’t mix white spaces and tabs

The length of a tab is 4 spaces but if you mix tabs and spaces in your python program you’ll most of the times get crazy errors. Though it might look same, it is not the same. Read on to know why. Only tabs or only spaces,  no mixing!

Concerning editors and the use of it

When you use editors for python, a simple one like notepad++ suffices or whatever editor which supports python.

Choose an editor which has the option to show spaces and tabs

As your editor will show the tabs, on the indentation error line, if all ok according to you, then check if the preceding lines’ identations were not done with spaces if you are using tabs or tabs if you are using spaces. That is true if you are working on someone else code.

Showing spaces and tabs in notepad++

Click on:

View –> show symbol –> Show white spaces and tabs

note

The result on notepad++

indent

See the spaces being marked in . . . . and the tabs as  . The colours etc. can be changed.

Geany the editor – allows for instant building

geany

Well, if you need to frequently check for errors, choose an editor which allows building and compiling. Then you won’t have to use command line often.

#Only then can you get an awesome py experience

Advertisements