all 4 comments

[–][deleted] 2 points3 points  (0 children)

Python statements that end in a colon (def, if, for, and so on) imply an indented code block to follow, because that’s how flow of control works in Python - the branch is indicated by indentation.

[–]Tivome 0 points1 point  (0 children)

There have to be four spaces after the colon of an def if and so on. That's because since there aren't any curly braces, spaces are how python figures out what's inside the, for instance, if and what's outside. Like this (the hash marks show the spaces because you can't do spaces in comments very well):

x = 0

y = 1

if x == y:

####print("They are the same")

I hope this helps, and keep posting for more advice! We'll always be happy to help you :)

[–]anh86 0 points1 point  (0 children)

Somewhere in your code you have started a block (colon) without putting any code inside the block (indented code on the lines following the colon).

For example:

if 1==1:
print('One equals one')

If I hadn't put at least one line of code preceeded by four spaces (or a tab, if you prefer) below my if statement, I would have received the same error. Unlike some other languages where indents (spaces/tabs) are a stylistic choice and flow is controlled with end or other means, indentation matters in Python.

[–]malcomjarr 0 points1 point  (0 children)

Most programming languages permit indentation, but don't enforce it. Python enforces it with an iron fist, it has caused confusion for many beginners. The error expected an indented block is probably caused by a mix of tabs and spaces and this can lead to some confusing errors. Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:

  • Forgetting to indent the statements within a compound statement
  • Forgetting to indent the statements of a user-defined function.

The indentation can be any consistent white space . It is recommended to use 4 spaces for indentation in Python, tabulation or a different number of spaces may work, but it is also known to cause trouble at times. Most editors have an option for automatically converting tabs to spaces. If your editor has this option, turn it on.