简体   繁体   English

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

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

I would like to use the following code from Pyomo Accessing solver status and termination conditions我想使用 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

Hoever, I get an error saying Expected an indented block at the elif .但是,我收到一条错误消息,说Expected an indented block at elif When inserting an indented block, I get the error Invalid syntax .插入缩进块时,出现错误Invalid syntax I posted a screenshot of both cases.我发布了这两种情况的屏幕截图。 I do not understand why I get this error?我不明白为什么会出现此错误? I just copied and pasted the code from the official pyomo website.我只是从 pyomo 官方网站复制并粘贴了代码。 Do you have any idea why I am getting this error and how I can get rid of it?您知道我为什么会收到此错误以及如何摆脱它吗?

在此处输入图像描述

You likely need to have at least 1 line of executable code within each if or elif block.您可能需要在每个ifelif块中至少包含 1 行可执行代码。 Right now, you just have a comment line.现在,您只有一个注释行。

While you are "shelling out" the program, just put the command pass in each block and see if that helps.当您“脱壳”程序时,只需将命令pass到每个块中,看看是否有帮助。 So:所以:

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

When code is laid out using whitespace like python, you need something actually in the block to show that it's there.当代码使用 python 之类的空格布局时,您需要实际在块中显示它的存在。 A comment isn't enough as these are ignored.评论是不够的,因为这些都被忽略了。

Your code currently looks like:您的代码当前如下所示:

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

The comment doesn't count as an indented block.注释不算作缩进块。

If you really have no code to put in there yet, you can use the no-op statement pass :如果你真的没有代码可以放在那里,你可以使用 no-op 语句pass

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

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

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