Python - 在某个if循环之后重复某个部分的代码

时间:2013-11-06 07:24:24

标签: python for-loop repeat

我正在为CFD可视化程序编写一个脚本,该程序有一个python接口。 我创建了一个通用对话框,用户可以在其中输入一些值。现在我想重复那个对话框,如果输入是假的(这意味着一个变量大于另一个变量):

items = []
items.append(['text',ITEM_TEXT,"Automatically save the evaluation in data file(s)\n",""])
items.append(['text',ITEM_TEXT,"Determine the range of timesteps that you want to save:",""])
items.append(['step_begin',ITEM_INT,"Start timestep:","Start timestep",first_step,first_step,timesteps])
items.append(['step_end',ITEM_INT,"End timestep:","End timestep",timesteps,first_step,timesteps])
a = CeiQtGenericDialog(items,None,"Save evaluation","Ok","Cancel")
ret = a.doit()

if (ret > 0):
    for i in a.getValues():
        if (i=='step_begin'):
            begin = a.getValue(i)
        if (i=='step_end'):
            end = a.getValue(i)

    if (begin > end):
        items2 = []
        items2.append(['text',ITEM_TEXT,"Start timestep must be smaller than end timestep!!",""])
        a = CeiQtGenericDialog(items2,None,"Error","Ok","Cancel")
        ret = a.doit()

        if (ret > 0):
            ????

a.getValues()中,之前存储了通用对话框的值。现在在最后,如果我想之前跳回到通用对话框。换句话说,我想重复从某一行开始的代码,这样用户可以在之前的通用对话框中重复输入“step_begin”和“step_end”,这怎么可能?

EDIT / 如果用户在通用对话框中单击“确定”,则在用户单击“取消”时获取值为1,获取值为0

2 个答案:

答案 0 :(得分:0)

我认为你应该重复整个这一部分,而你却没有得到结果你需要

while True:
    a = CeiQtGenericDialog(items,None,"Save evaluation","Ok","Cancel")
    ret = a.doit()

    if (ret < 0): 
        break # if cancel dialog will not proceed data analysis
    for i in a.getValues():
        if (i=='step_begin'):
            begin = a.getValue(i)
        if (i=='step_end'):
            end = a.getValue(i)

    if (begin < end):
        break # if everything is ok move out 
    items2 = []
    items2.append(['text',ITEM_TEXT,"Start timestep must be smaller than end timestep!!",""])
    a = CeiQtGenericDialog(items2,None,"Error","Ok","Cancel")
    ret = a.doit()
        if ret < 0:
           break # if cancel dialog will not proceed data analysis

答案 1 :(得分:0)

我会使用while循环,如下所示:

begin = 0
end = -1
ret = 1
while (begin > end and ret > 0):
    items = []
    items.append(['text',ITEM_TEXT,"Automatically save the evaluation in data file(s)\n",""])
    items.append(['text',ITEM_TEXT,"Determine the range of timesteps that you want to save:",""])
    items.append(['step_begin',ITEM_INT,"Start timestep:","Start timestep",first_step,first_step,timesteps])
    items.append(['step_end',ITEM_INT,"End timestep:","End timestep",timesteps,first_step,timesteps])
    a = CeiQtGenericDialog(items,None,"Save evaluation","Ok","Cancel")
    ret = a.doit()

    if (ret > 0):
        for i in a.getValues():
            if (i=='step_begin'):
                begin = a.getValue(i)
            if (i=='step_end'):
                end = a.getValue(i)

        if (begin > end):
            items2 = []
            items2.append(['text',ITEM_TEXT,"Start timestep must be smaller than end timestep!!",""])
            a = CeiQtGenericDialog(items2,None,"Error","Ok","Cancel")
            ret = a.doit()