简体   繁体   中英

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

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 . When inserting an indented block, I get the error 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. 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. 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. 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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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