【问题标题】:Python: IndentationError [duplicate]Python:缩进错误 [重复]
【发布时间】:2018-10-11 12:58:21
【问题描述】:

我在使用以下代码时出现缩进错误,我该如何解决?

from sklearn.linear_model import LogisticRegression

def classification_model(model, data, predictors, outcome):

outcome_var = 'LoanAmount'

model = DecisionTreeClassifier()

predictor_var = ['Credit_History','Gender','Married','Education']

classification_model(model, df,predictor_var,outcome_var)

*文件“”,第 3 行 结果_var = '贷款金额'

        ^ IndentationError: expected an indented block*

【问题讨论】:

  • 函数体内的任何东西都需要缩进
  • 好吧,按照错误提示在函数中缩进代码
  • 你没有那个函数的主体。
  • 我投票决定将此问题作为离题结束,因为解决方案直接来自文档

标签: python


【解决方案1】:

在 python 中,范围是使用缩进定义的

你有一个用def classification_model(model, data, predictors, outcome): 定义的函数,所以这个函数需要一些语句。 outcome_var='LoanAmount' 语句需要缩进才能包含在函数中,属于该函数的所有其他语句必须遵循相同的缩进

例如:

def function_with_no_statements():
    pass

print("Here")

def print_something(something):
    print(something)

print("Here 2")

print_something("Here 3")

如果您运行上面的示例,您将看到 print("Here") 语句不在任何函数中,还有 print("Here 2"),如果您调用 print_something 函数,它将打印您发送的任何内容(也就是说执行缩进语句

【讨论】:

  • 没有什么需要缩进的,outcome_var是作为参数传入的变量之一。
猜你喜欢
  • 2013-02-05
  • 2014-08-12
  • 2011-05-14
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多