簡體   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