簡體   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