3

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
    ans=0
      ^
IndentationError: expected an indented block

so what is the wrong thing i'm doing

5

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

Try:

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

with 2 before ans=0.

Read here for complete documentatio of python.

| improve this answer | |
  • can i make it by default use the spaces or the TABs just like the shell in windows – Fat Mind May 20 '14 at 5:47
  • You can use spaces or tabs as you like, it makes no difference to python interpreter. – girardengo May 20 '14 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 '14 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 '14 at 6:18
0

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'

OR

'Convert Indentation to Tabs'

Your code will work in either case.

| improve this answer | |

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.