Python IndentationError:Xcode 上的“应为缩进块”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14966531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:48:38  来源:igfitidea点击:

Python IndentationError: “expected an indented block” on Xcode

pythonxcodeindentation

提问by snazziii

I've been getting this error on xcode and vi. Python says the line class LeastModel has an IndentationError: expected an indented block. I checked my preferences on Xcode to use 4 spaces for a tab and everywhere I've been using tab. Please help me!

我一直在 xcode 和 vi 上收到此错误。Python 表示行类 LeastModel 有一个 IndentationError: expected an indented block。我检查了我在 Xcode 上的首选项,以使用 4 个空格作为选项卡以及我一直使用选项卡的所有地方。请帮我!

def make_model(data,model):

class LeastModel():
    """
    linear system solved using linear least squares
    This class serves as an example that fulfills the model interface needed by the ransa function.
    """
    def __init__(self,input_columns,output_columns):
        self.input_columns = input_columns
        self.output_columns = output_columns
        #self.debug = debug

回答by David Robinson

Your problem is that you have no indented code after the line:

您的问题是在该行之后没有缩进代码:

def make_model(data,model):

You can either:

您可以:

  1. Get rid of that line

  2. Write some indented code into the body of that function

  3. Indent your entire class definition so that you are defining the class LeastModelwithin the function.

  1. 摆脱那条线

  2. 将一些缩进的代码写入该函数的主体中

  3. 缩进整个类定义,以便LeastModel在函数内定义类。

Judging by the fact that you called your function make_modeland your class LeastModel, I think you somehow intended to put the class definition within the function. But this might be a mistake on your part- note that if you define it in a function, you won't be able to use the class outside of the function (unless you return the class itself from the function, with the line return LeastModel.

从您调用函数make_model和类的事实来看LeastModel,我认为您打算以某种方式将类定义放在函数中。但这可能是您的错误 - 请注意,如果您在函数中定义它,您将无法在函数外部使用该类(除非您从函数返回类本身,使用return LeastModel.

回答by Andrew Clark

Assuming there wasn't a copy error and that is what your code actually looks like, you need to indent __init__()so that it is inside of the class definition:

假设没有复制错误并且这就是您的代码实际的样子,您需要缩进__init__()以使其位于类定义内:

class LeastModel():
    """
    linear system solved using linear least squares
    This class serves as an example that fulfills the model interface needed by the ransa function.
    """
    def __init__(self,input_columns,output_columns):
        self.input_columns = input_columns
        self.output_columns = output_columns
        #self.debug = debug

edit:Now that you have included the full code, the issue is actually that you have nothing under your make_model()function definition. If that function is actually supposed to do nothing, add passbeneath the defline (indented one level). Otherwise, put some code there or remove the defline.

编辑:既然您已经包含了完整的代码,问题实际上是您的make_model()函数定义下没有任何内容。如果该函数实际上什么都不做,请pass在该def行下方添加(缩进一级)。否则,将一些代码放在那里或删除该def行。

回答by dln385

Shouldn't it be:

不应该是:

class LeastModel():
    """
    linear system solved using linear least squares
    This class serves as an example that fulfills the model interface needed by the ransa function.
    """
    def __init__(self,input_columns,output_columns):
        self.input_columns = input_columns
        self.output_columns = output_columns
        #self.debug = debug