4

I tried this code at the interpreter prompt:

>>> x = 7
>>> if x > 5:
... print("five")

I got this error message:

File "<stdin>", line 2
    print("five") 
IndentationError: expected an indented block

Why?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
pizzapablo
  • 107
  • 1
  • 3
  • 12
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named "[What topics can I ask about here?](http://stackoverflow.com/help/on-topic)" and "[What types of questions should I avoid asking?](http://stackoverflow.com/help/dont-ask)". Also please [take the tour](http://stackoverflow.com/tour) and read about [how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – ArturFH Jun 12 '17 at 15:44

2 Answers2

8

In the shell you should do this:

>>> if x > 5:
...    print("five")

That is, add an indent block after the ":"

DarkCygnus
  • 7,420
  • 4
  • 36
  • 59
  • No problem @ManeHambardzumyan – DarkCygnus Jun 12 '17 at 15:56
  • I have added an indentation block but it doesn't end when I press enter again – ikamen Feb 05 '21 at 11:03
  • @ikamen you must be missing some parenthesis or something like that. If your parentheses are ok it should execute the command. If that is not the case, perhaps you are missing another enter. Usually it's two enters: one for the next line and another to tell python "hey I'm done, please execute" – DarkCygnus Feb 05 '21 at 21:22
4

Indentation is important in Python. You need to indent your print statement with a few spaces or a tab otherwise the interpreter will think your if is empty.

>>> if x > 5:
...    print("five")
MrPromethee
  • 721
  • 9
  • 18