I started to learn python , but when I try to make an function I get this error

IndentationError: expected an indented block

I use the terminal and write python then start to write the function like that

>>> def mul (x,y) :
... ans=0
  File "<stdin>", line 2
IndentationError: expected an indented block

so what is the wrong thing i'm doing

2 Answers 2


Python uses indentation to determine the grouping of statements. You can use one or more spaces or one or more tab, just that within the same statement always use one or the other. Is recommended to use spaces instead of tabs, but is not required. Read here


>>> def mul (x,y) :
...   ans = 0
...   print ans

with 2 before ans=0.

Read here for complete documentatio of python.

  • can i make it by default use the spaces or the TABs just like the shell in windows
    – Fat Mind
    May 20, 2014 at 5:47
  • You can use spaces or tabs as you like, it makes no difference to python interpreter.
    – girardengo
    May 20, 2014 at 5:59
  • I mean , I don't want to write it my self , I need it to be written by the shell by default
    – Fat Mind
    May 20, 2014 at 6:01
  • 1
    python console by default doesn't offer tab completion. You can use another interactive interpreter as ipython, or create a script with text-editor that support indentation (eg: gedit,emacs,geany)
    – girardengo
    May 20, 2014 at 6:18

Indentation Error generally occurs when there is inconsistency in the code means somewhere we have used spaces and somewhere tabs for indentation. I have got a very nice solution to this, just open your code in sublime text editor and find 'Tab Size' in bottom right corner of Sublime Text Editor and click it. Now select either

'Convert Indentation to Spaces'


'Convert Indentation to Tabs'

Your code will work in either case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.