How to fix IndentationError: “expected an indented block”?

落爺英雄遲暮 提交于 2021-02-05 12:17:25

问题


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

回答1:


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)



回答2:


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


来源:https://stackoverflow.com/questions/41131768/how-to-fix-indentationerror-expected-an-indented-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!