2

When I run my python program, unexpected "expected an indented block" errors keep popping up. I don't see anything wrong with the code, so please help.

def function
    if mode == 1:
    #code
    elif mode == 2:
    #code
    else:
    #code

while True:
    while True:
        #code here
6
  • Your first three #code comments, when replaced with code, would need to be indented with respect to the preceding if, elif, or else. If they are lined up with them, as they are now, then the bodies are empty and it's a syntax error. Commented Jan 27, 2016 at 5:10
  • 4
    You are missing a : after def function. Commented Jan 27, 2016 at 5:13
  • Could you please include your full code, exactly as it appears in your text editor? Commented Jan 27, 2016 at 5:21
  • you have to put some code in place of #code - at least pass command. if/elif/else/while/for can't have empty blocks - comments are not treated as correct blocks. Commented Jan 27, 2016 at 5:25
  • @BurhanKhalid: Hmm...which should raise SyntaxError: invalid syntax then. So I think it's just a copy-paste issue. Commented Jan 27, 2016 at 5:38

3 Answers 3

5

Each time you type : at the end of the line, Python expects a statement or expression indented in the next block.

To create an "empty" loop, use pass:

def function():
    if mode == 1:
        pass
        # code will go here
    elif mode == 2:
        pass
        # code will go here
    else:
        pass
        # code will go here

while True:
    while True:
        pass
        # code here

the error occurs at the first "While True" loop

The reason it happens, is because after the else: Python is expecting a statement or an expression, and since the first one of those is the while True:, and its not indented to be under the else: block you get that exception.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all, after def function there must be parenthesis followed by parenthesis and a colon. I'm not sure, but are you indenting after the if statements. AIs there legit code where your commenting or is that just a comment? If there is no code, it will raise an error due to the fact that there must be an indented block after a control line. For example,

def Function(mode):
    if mode == 0:
        print "mode is 0"
    elif mode == 1:
            print "mode is 1"
    else:
        print "unknown mode"
while True:
     while True:
         Function(5)
         #do What ever here

This will not raise an Exception

IMPORTANT: Make sure you break your code otherwise it will go forever.

Comments

0

I figured out the problem, but thanks anyway. Python needs you to put code in the if and else statements or else it will think that you forgot to indent in the rest of the code. But thanks!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.