繁体   English   中英

不明白为什么 Pyomo 出现“预期缩进块”错误

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

我想使用 Pyomo 中的以下代码访问求解器状态和终止条件

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

但是,我收到一条错误消息,说Expected an indented block at elif 插入缩进块时,出现错误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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM