Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PYTHON-重新启动相同的循环迭代_Python_For Loop - Fatal编程技术网

PYTHON-重新启动相同的循环迭代

PYTHON-重新启动相同的循环迭代,python,for-loop,Python,For Loop,我需要一些帮助 我有这个循环 for i in range(0,15): logging.info('----- packing iteration %i started ------' % (i)) po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration po_count

我需要一些帮助

我有这个循环

for i in range(0,15):
        logging.info('-----  packing iteration %i started  ------' % (i))
        po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
        po_count = 0
        for po_list in po_selection_list: # loop through all lists
            if len(po_list) == 0: #if list is empty go to next list
                pass
            else: # else load po's in tray
                po_count += len(po_list)
                functions_library.AddPOs_Stage1(po_list,driver)
                functions_library.AddPOs_trayIDSearch(driver)
                functions_library.AddPOs_Stage2(driver)

                functions_library.ImportParts()
        if po_count == 0: # if no po's were loaded in the tray go to next iteration
            pass
        else: # else pack and sync in netfabb
            functions_library.MovePartsZHeight()
            functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
            functions_library.RemoveNetfabbExtraParts()
            functions_library.NetfabbSync(driver)
想象一下,如果我们在第4次迭代中运行。 我需要的是如果functions\u library.AddPOs\u trayIDSearch(driver)返回False我想再次重新启动循环(迭代次数4)

编辑

for i in range(0,15):
    logging.info('-----  packing iteration %i started  ------' % (i))
    po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
    po_count = 0
    for po_list in po_selection_list: # loop through all lists
        if len(po_list) == 0: #if list is empty go to next list
            pass
        else: # else load po's in tray
            po_count += len(po_list)
            functions_library.AddPOs_Stage1(po_list,driver)
            functions_library.AddPOs_trayIDSearch(driver)
            functions_library.AddPOs_Stage2(driver)

            functions_library.ImportParts()
    if po_count: # if no po's were loaded in the tray go to next iteration
        functions_library.MovePartsZHeight()
        functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
        functions_library.RemoveNetfabbExtraParts()
        functions_library.NetfabbSync(driver)

### finish the tray ###
functions_library.SelectAll(TrayHeight)
functions_library.MovePartsZHeight()
functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
functions_library.RemoveNetfabbExtraParts()
functions_library.RemoveCylinders(TrayHeight)
functions_library.NetfabbSync(driver)
functions_library.SetToPrinting(driver)
functions_library.SaveJob(tray_folder,TrayName)
functions_library.NetfabbSlicing(TrayID,tray_folder,TrayName)
logging.info('Tray: '+TrayName+' done!')

在使用迭代器时使用
不是更容易吗

c = 0
while c < 5:
    ...
    result = functions_library.AddPOs_trayIDSearch(...)
    if not result:
        c = 0
    else:
        c -= 1
c=0
c<5时:
...
结果=函数\u library.AddPOs\u trayIDSearch(…)
如果没有结果:
c=0
其他:
c-=1

在回答之前,请注意:

if something == 0:
    pass
else:
    do_stuff()
可简化为:

if something:
    do_stuff()
因为Python将其解释为如果bool(something)=True:do_stuff()

对于实际问题,您可以使用
while
循环并递增计数器,前提是您感兴趣的调用返回
True
。您可以使用构造检查每个调用是否成功:

i = 0
while i < 15:
    logging.info('-----  packing iteration %i started  ------' % (i))
    po_selection_list = po_sel.get_po_lists(TrayID,TrayHeight,Materials,TrayName,i) # get po lists for this iteration
    po_count = 0
    for po_list in po_selection_list: # loop through all lists
        if po_list: # load po's in tray
            po_count += len(po_list)
            functions_library.AddPOs_Stage1(po_list,driver)
            if not functions_library.AddPOs_trayIDSearch(driver):
                break
            functions_library.AddPOs_Stage2(driver)

            functions_library.ImportParts()
    else:
        # break didn't occur
        if po_count: # pack and sync in netfabb
            functions_library.MovePartsZHeight()
            functions_library.NetfabbPacking(TrayID,TrayHeight,Materials)
            functions_library.RemoveNetfabbExtraParts()
            functions_library.NetfabbSync(driver)
        i += 1
i=0
而我<15:
logging.info('----打包迭代%i已开始---'%(i))
采购订单选择列表=采购订单选择。获取采购订单列表(TrayID、TrayHeight、Materials、TrayName、i)#获取此迭代的采购订单列表
采购订单数量=0
对于采购订单选择列表中的采购订单列表:#循环浏览所有列表
如果订单列表:#将订单装入托盘
采购订单数量+=len(采购订单列表)
函数库。添加位置1级(位置列表,驱动程序)
如果不是函数\u library.AddPOs\u trayIDSearch(驱动程序):
打破
函数\u library.AddPOs\u Stage2(驱动程序)
函数库.ImportParts()
其他:
#没有发生断裂
如果po_计数:#在netfabb中打包并同步
函数\u library.MovePartsZHeight()
函数库.NetfabbPacking(TrayID、TrayHeight、Materials)
函数\u library.RemoveNetfabbExtraParts()
函数库.NetfabbSync(驱动程序)
i+=1

您是否正在寻找
继续
?不,因为我不想跳到迭代5,所以我想重新运行迭代4。我不知道变量
驱动程序
来自哪里。它是否每次迭代都会改变它的值?提供的代码示例没有意义,因为如果
driver
的值不改变,您将创建一个无止境的循环,一直迭代4次。如果a!=0:do_stuff()。开头有一个小错误。@cezar
如果a:do\u stuff()
如果a!=0:do_stuff()
,因为它考虑所有真实值,而不仅仅是整数。检查代码中的
if po_list
以了解。我的意思是
something
,只是将
a
作为快捷方式。为了简化if子句,你必须否定条件。是的,你是对的。当与0比较时,我们可以检查truthy(或falsy)值。我的想法是一般性的,而不是特别关注0。但是if子句中的
pass
立即引起了我的注意。它肯定不属于那里。指出这一点很好。这个解决方案的问题是,我只会打破订单列表的
。我还希望
while循环
停止该迭代并重新开始。我想这还不完全清楚,因为我遗漏了一些代码。我现在就编辑这篇文章