How do I rectify the error \"unexpected indent\" in python?
Turn on visible whitespace in whatever editor you are using and turn on replace tabs with spaces.
While you can use tabs with Python mixing tabs and space usually leads to the error you are experiencing. Replacing tabs with 4 spaces is the recommended approach for writing Python code.
There is a trick that always worked for me:
If you got and unexpected indent and you see that all the code is perfectly indented, try opening it with another editor and you will see what line of code is not indented.
It happened to me when used vim, gedit or editors like that.
Try to use only 1 editor for your code.
Simply copy your script and put under """ your entire code """ ...
specify this line in a variable.. like,
a = """ your python script """
print a.replace('here please press tab button it will insert some space"," here simply press space bar four times")
# here we replacing tab space by four char space as per pep 8 style guide..
now execute this code, in Sublime Editor using ctrl+b, now it will print indented code in console. that's it
If the indentation looks ok then have a look to see if your editor has a "View Whitespace" option. Enabling this should allow to find where spaces and tabs are mixed.
In Python, the spacing is very important, this gives the structure of your code blocks. This error happens when you mess up your code structure, for example like this :
def test_function() :
if 5 > 3 :
print "hello"
You may also have a mix of tabs and spaces in your file.
I suggest you use a python syntax aware editor like PyScripter, or Netbeans
Make sure you use the option "insert spaces instead of tabs" in your editor. Then you can choose you want a tab width of, for example 4. You can find those options in gedit under edit-->preferences-->editor.
bottom line: USE SPACES not tabs