Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "expected an indented block"

Tags:

python

Let me start off by saying that I am COMPLETELY new to programming. I have just recently picked up Python and it has consistently kicked me in the head with one recurring error -- "expected an indented block" Now, I know there are several other threads addressing this problem and I have looked over a good number of them, however, even checking my indentation has not given me better results. I have replaced all of my indents with 4 spaces and even rewritten the code several times. I'll post this counter assignment I got as an example.

option == 1 while option != 0:     print "MENU"     option = input()     print "please make a selection"     print "1. count"     print "0. quit"     if option == 1:         while option != 0:             print "1. count up"             print "2. count down"             print "0. go back"             if option == 1:                 print "please enter a number"                 for x in range(1, x, 1):                     print x                 elif option == 2:                     print "please enter a number"                     for x in range(x, 1, 1):                 elif option == 0:                     break                 else:                     print "invalid command"     elif option == 0:         break 
like image 339
Zach Avatar asked Aug 01 '11 16:08

Zach


People also ask

How do you fix an expected indented block in Python?

To resolve expected an indented block in Python, correct the indentation of each block. Before Python runs any code in your program, it will first discover the correct parent and children of each line. Python throws an Indentation whenever it comes across a line for which it cannot define the right parent to assign.

What does expected an indented block mean Python?

expected an indented block This means that a function must have at least one line of code. It also means that a conditional must have at least one line of code to run if the condition is true.

How do you fix an unexpected indent block?

“IndentationError: unexpected indent” is raised when you indent a line of code too many times. To solve this error, make sure all of your code uses consistent indentation and that there are no unnecessary indents.

What's an indented block?

In general, a block indent is multiple lines of text that are indented. Most programs and websites that indent text block indent the paragraph or all text following the first line, unless it is a first-line indent or hanging indent.


1 Answers

Starting with elif option == 2:, you indented one time too many. In a decent text editor, you should be able to highlight these lines and press Shift+Tab to fix the issue.

Additionally, there is no statement after for x in range(x, 1, 1):. Insert an indented pass to do nothing in the for loop.

Also, in the first line, you wrote option == 1. == tests for equality, but you meant = ( a single equals sign), which assigns the right value to the left name, i.e.

option = 1 
like image 108
phihag Avatar answered Sep 23 '22 18:09

phihag