PYTHON - 重复循环迭代

时间:2015-09-29 09:34:44

标签: python for-loop

我需要一些帮助。

我有这个for循环

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_library.AddPOs_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!')

2 个答案:

答案 0 :(得分:2)

在回答之前,请注意:

if something == 0:
    pass
else:
    do_stuff()

可以简化为:

if something:
    do_stuff()

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

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

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

答案 1 :(得分:0)

使用while迭代器是不是更容易?

c = 0
while c < 5:
    ...
    result = functions_library.AddPOs_trayIDSearch(...)
    if not result:
        c = 0
    else:
        c -= 1