简体   繁体   中英

How to fix IndentationError: "expected an indented block"?

I get an error

IndentationError: expected an indented block

in line line 3

answer = subprocess.check_output(['/home/dir/final/3.sh'])

My code is:

import subprocess
while True:
answer = subprocess.check_output(['/home/dir/final/3.sh'])
final = int(answer) // int('1048576')
print final

In documentation terminology, indentation means the space from margin to the begin of characters in a line.

Python uses indentation. In your code, While is a condition, all the block of code to be executed for true, must start at same position from the margin, must be farther away from margin than that of the condition.

I too faced this error.

Example:

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    if a>=1 and a<=10000000000 and b>=1 and b<=10000000000:
    print(a+b)
    print(a-b)
    print(a*b)

will throw "IndentationError: expected an indented block (solution.py, line 5)"

Fix:

if __name__ == '__main__':
    a = int(input())
    b = int(input())
if a>=1 and a<=10000000000 and b>=1 and b<=10000000000:
    print(a+b)
    print(a-b)
    print(a*b)

Python requires indentation to indicate code that is conditional under for loops, while loops, if and else statements, etc. Generally, this is code that runs contingent on logic before a colon.

Most coders use four spaces for indentation.

Tabs are a bad idea because they may create different amount if spacing in different editors. They're also potentially confusing if you mix tabbed indentation with spaced indentation. If you're using an IDE like Eclipse or Eric, you can configure how many spaces the IDE will insert when you press tab.

I think your code should be:

import subprocess 

while True: 
    answer = subprocess.check_output(['/home/dir/final/3.sh']) 
final = int(answer) // int('1048576')
print final

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM