【问题标题】:Why am I getting "IndentationError: expected an indented block"? [duplicate]为什么我会收到“IndentationError: expected an indented block”? [复制]
【发布时间】:2010-12-15 03:12:59
【问题描述】:
if len(trashed_files) == 0 :
    print "No files trashed from current dir ('%s')" % os.path.realpath(os.curdir)
else :
    index=raw_input("What file to restore [0..%d]: " % (len(trashed_files)-1))
    if index == "*" :
        for tfile in trashed_files :
            try:
                tfile.restore()
            except IOError, e:
                import sys
                print >> sys.stderr, str(e)
                sys.exit(1)
    elif index == "" :
        print "Exiting"
    else :
        index = int(index)
        try:
            trashed_files[index].restore()
        except IOError, e:
            import sys
            print >> sys.stderr, str(e)
            sys.exit(1)

我得到:

        elif index == "" :
        ^
    IndentationError: expected an indented block

【问题讨论】:

标签: python


【解决方案1】:

如错误消息所示,您有一个缩进错误。这可能是由制表符和空格的混合引起的。

【讨论】:

  • 我首先想到的是,所以我将所有制表符转换为 4 个空格。
  • @BHare:然后……那没有解决?我刚刚复制并粘贴了您的代码,并添加了一行 trashed_files = ['a','b'] 以便它运行...没有给我任何缩进错误。
  • 我再次进行了替换,然后我重写了 if len(trashed_files) == 0 : 行,因为它在 stackoverflow 上很奇怪...原来是问题所在...不知道为什么/什么。在 nano 中看起来是一样的。
  • @BH:看,标签很危险! :p
  • @katrielalex 没错,即使我遇到了同样的错误,这篇文章也帮助了我......谢谢大家
【解决方案2】:

事实上,关于 Python 中的缩进,你需要了解很多事情:

Python 真的很关心缩进。

在许多其他语言中,缩进不是必需的,但可以提高可读性。在 Python 中,缩进取代了关键字 begin / end{ },因此是必要的。

这是在执行代码之前验证的,因此即使从未到达缩进错误的代码,它也不会工作。

有不同的缩进错误,阅读它们会有很大帮助:

1. "IndentationError: 需要一个缩进块"

它们是您可能出现此类错误的两个主要原因:

- 你有一个“:”,后面没有缩进块。

这里有两个例子:

示例 1,无缩进块:

输入:

if 3 != 4:
    print("usual")
else:

输出:

  File "<stdin>", line 4

    ^
IndentationError: expected an indented block

输出表明您需要在第 4 行,else: 语句之后有一个缩进块

示例 2,未缩进的块:

输入:

if 3 != 4:
print("usual")

输出

  File "<stdin>", line 2
    print("usual")
        ^
IndentationError: expected an indented block

输出表明您需要在 if 3 != 4: 语句之后的第 2 行缩进块

- 您正在使用 Python2.x 并且混合了制表符和空格:

输入

def foo():
    if 1:
        print 1

请注意 if 前有一个制表符,print 前有 8 个空格。

输出:

  File "<stdin>", line 3
    print 1
      ^
IndentationError: expected an indented block

很难理解这里发生了什么,似乎有一个缩进块......但正如我所说,我使用了制表符和空格,你不应该这样做。

  • 你可以得到一些信息here
  • 删除所有制表符并将它们替换为四个空格。
  • 并将您的编辑器配置为自动执行此操作。

2。 “IndentationError:意外缩进”

缩进块很重要,但只有应该缩进的块。 所以基本上这个错误说:

- 您有一个缩进块,前面没有“:”。

示例:

输入:

a = 3
  a += 3

输出:

  File "<stdin>", line 2
    a += 3
    ^
IndentationError: unexpected indent

输出表明他不期望缩进块第 2 行,那么你应该删除它。

3. “TabError:缩进中制表符和空格的使用不一致”(仅限python3.x)

  • 你可以得到一些信息here
  • 但基本上是,您在代码中使用制表符和空格。
  • 你不想这样。
  • 删除所有制表符并将它们替换为四个空格。
  • 并将您的编辑器配置为自动执行此操作。


最终,回到你的问题:

只需查看错误的行号,并使用以前的信息进行修复。

【讨论】:

  • 这是我找到的理解它的最佳答案(#2 确定了它)。谢谢!
  • 很好的答案!我被之前的 else: 块发现了,该块只包含注释掉的行,导致下一个块出现这种情况 2 错误。
【解决方案3】:

我遇到了同样的问题并发现(通过this answer to a similar question)问题在于我没有正确缩进文档字符串。不幸的是,IDLE 在这里没有提供有用的反馈,但是一旦我修复了文档字符串的缩进,问题就消失了。

特别是 --- 产生缩进错误的错误代码:

def my_function(args):
"Here is my docstring"
    ....

避免缩进错误的好代码:

def my_function(args):
    "Here is my docstring"
    ....

注意:我并不是说这问题,而是它可能是,因为就我而言,它是!

【讨论】:

    【解决方案4】:

    在python预期块中意味着在我的情况下,每件事都必须以这种方式编写,我是这样写的

     def btnClick(numbers):
     global operator
     operator = operator + str(numbers)
     text_input.set(operator)
    

    Note.it 给我错误,直到我以这种方式编写它以便“给空格”然后它给我一个块,因为我试图在代码下面的函数中向你展示

    def btnClick(numbers):
    ___________________________
    |global operator
    |operator = operator + str(numbers)
    |text_input.set(operator)
    

    【讨论】:

      【解决方案5】:

      您可能需要检查空格和制表符。制表符默认为 4 个空格。但是,您的“if”和“elif”匹配,所以我不太清楚为什么。进入顶部栏中的选项,然后单击“配置空闲”。检查字体/制表符右侧的缩进宽度,并确保您的缩进有那么多空格。

      【讨论】:

      • 使用“IDLE”帮助编辑。
      【解决方案6】:

      这只是一个缩进问题,因为 Python 对此非常严格。

      如果你使用Sublime,你可以全选,点击“Python”旁边的右下角,确保选中“Indent using spaces”并选择你的Tab Width以保持一致,然后Convert Indentation to Spaces 将所有制表符转换为空格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        • 1970-01-01
        • 2021-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-20
        相关资源
        最近更新 更多