繁体   English   中英

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

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

我收到一个错误

IndentationError:需要一个缩进块

在第 3 行

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

我的代码是:

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

在文档术语中,缩进是指从边距到一行中字符开头的空间。

Python 使用缩进。 在你的代码中,While 是一个条件,所有要执行为真的代码块,必须从边距相同的位置开始,必须比条件离边距更远。

我也遇到了这个错误。

例子:

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)

将抛出“IndentationError:应为缩进块(solution.py,第 5 行)”

使固定:

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 需要缩进来指示在 for 循环、while 循环、if 和 else 语句等条件下的代码。通常,这是在冒号之前根据逻辑运行的代码。

大多数编码员使用四个空格来缩进。

制表符是一个坏主意,因为如果在不同的编辑器中间隔,它们可能会创建不同的数量。 如果将制表式缩进与间隔缩进混合使用,它们也可能会造成混淆。 如果您使用的是 Eclipse 或 Eric 等 IDE,您可以配置当您按 Tab 键时 IDE 将插入多少个空格。

我认为你的代码应该是:

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