简体   繁体   English

如何修复 IndentationError:“需要一个缩进块”?

[英]How to fix IndentationError: "expected an indented block"?

I get an error我收到一个错误

IndentationError: expected an indented block IndentationError:需要一个缩进块

in line line 3在第 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. Python 使用缩进。 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.在你的代码中,While 是一个条件,所有要执行为真的代码块,必须从边距相同的位置开始,必须比条件离边距更远。

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)"将抛出“IndentationError:应为缩进块(solution.py,第 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. Python 需要缩进来指示在 for 循环、while 循环、if 和 else 语句等条件下的代码。通常,这是在冒号之前根据逻辑运行的代码。

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.如果您使用的是 Eclipse 或 Eric 等 IDE,您可以配置当您按 Tab 键时 IDE 将插入多少个空格。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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