不明白为什么我使用 Pyomo 时会出现 "Expected an indented block" 错误

Don't understand why I get an "Expected an indented block" error with Pyomo

我想使用 Pyomo 的以下代码 Accessing solver status and termination conditions

results = opt.solve(instance) # Solving a model instance  
instance.load(results) # Loading solution into results object

if (results.solver.status == SolverStatus.ok) and (results.solver.termination_condition == TerminationCondition.optimal):
    # Do something when the solution in optimal and feasible
elif (results.solver.termination_condition == TerminationCondition.infeasible):
    # Do something when model in infeasible
else:
    # Something else is wrong
    print “Solver Status: ”,  result.solver.status

但是,我在 elif 处收到一个错误提示 Expected an indented block。插入缩进块时,出现错误 Invalid syntax。我发布了两种情况的屏幕截图。我不明白为什么会出现此错误?我刚刚从 pyomo 官方网站复制并粘贴了代码。您知道我为什么会收到此错误以及如何消除它吗?

您可能需要在每个 ifelif 块中至少包含 1 行可执行代码。现在,您只有一个评论行。

当您“敲定”程序时,只需将命令 pass 放入每个块中,看看是否有帮助。所以:

if (something >= something_else):
    # do something
    pass
else:
    # do the other thing
    pass
....

当使用像 python 这样的空格来布置代码时,您需要在块中实际放置一些东西来表明它在那里。评论是不够的,因为这些被忽略了。

您的代码目前看起来像:

if ... :
    # comment where block should be
elif ... :
    print "something"

评论不算作缩进块。

如果你真的没有代码可以放在那里,你可以使用 no-op 语句 pass:

if ... :
    # todo
    pass
elif ... :
    print "something"