【问题标题】:python how to pass parameter from later loop back to before loop and outer looppython如何将参数从后循环传递回前循环和外循环
【发布时间】:2021-12-30 02:42:09
【问题描述】:

喜欢这种循环模式:

for a in (a_start, a_end):
    for b in (b_start, b_end):
        if condition_b1:
            action_b1
        else:
            action_b2
    
    for c in (c_start,c_end):
        if condition_c1:
            action_c1
        else:
            action_c2 (pass a new a_start and b_start back)

如上所示,我需要将一些新设置的参数传回前循环和外循环。 怎么做? 我正在等待答案或讨论。 我需要在断点之后继续使用新设置的参数。 看来我需要将参数传递回最开始。

原代码如下:

id_number = 19
page_number_start = 4
auto_proxy_verification = 1
last_proxy_not_enough = 0

my_proxy = ['***************',
         '*****************',
         '**************',
         '**********',
         '****************',
         ]

proxy_number_start = 0
    for page_number in range(page_number_start, 999):
    login_url = f'https://***********&pageNumber={page_number}&**********'
    if proxy_number_start >= len(my_proxy): break
    for proxy_number in range(proxy_number_start, len(my_proxy)):
        try:
            driver.get(login_url)
            all_divs =       driver.find_elements_by_class_name('****classname1****')
            locked_divs = driver.find_elements_by_xpath("****xpath1****")
            unlocked_divs =     
driver.find_elements_by_xpath("****xpath2****")
            if len(unlocked_divs) == 0:  # bad condition 1
                print('proxy not active')
                proxy_number_start = proxy_number_start + 1
            elif len(all_divs) == 10: # bad condition 2
                print('proxy not enough')
                proxy_number_start = proxy_number_start + 1
                if proxy_number_start == len(my_proxy):  ### this line is only for the last proxy
                    last_proxy_not_enough = 1
                    break
            else: # good condition
                print('proxy ok')
                proxy_number_start = proxy_number_start + 1
                break
        except:  ### if can not open the page ,means the proxy is bad
            print('proxy bad')
            proxy_number_start = proxy_number_start + 1
    main_window = driver.current_window_handle

id_number_in = 0
for div in all_divs:  ### for-loop inside the page
    if last_proxy_not_enough == 1: break
    id_number_in = id_number_in + 1
    if id_number_in < id_number:pass
    else:
        id_number = id_number + 1
        if id_number >= len(all_divs):break
        detail_body_number = 'detail-body-%d' % id_number
        first_div_class = driver.find_element_by_xpath('//*[@id="{}"]/***/div[1]'.format(detail_body_number)).get_attribute('class')
        if first_div_class == '****certainclassname****': ## ok condition
                action = ActionChains(driver)
                action.key_down(Keys.CONTROL).click(title_href).key_up(Keys.CONTROL).perform()
                driver.switch_to.window(driver.window_handles[-1])
                try:
                    download_button = driver.find_element_by_xpath('****downloadbutton*******').click()
                    print('%d is unlocked, and downloaded' % id_number)
                    time.sleep(0.5)
                except:
                    try:  # if need purchase
                        purchase_button_text = driver.find_element_by_xpath('****purchasebutton*******').text
                        if purchase_button_text == 'Purchase':
                            print('here purchase button!!!' % id_number)
                    except:
                        print('sometimes the proxy turns to be not active')
                        # !!!!!!!!!!!! need to pass back the parameter "page_number_start" and "id_number"
                        page_number_start = page_number_start - 1  #!!!!!!!!!!!!!!!!!task
                        id_number = id_number_in   #!!!!!!!!!!!!!!!!!task
                        break
                try:
                    driver.find_element_by_xpath('****downloadbutton*******').click()
                    time.sleep(2)
                except:
                    pass
        else:
            print('%d is locked, pass' % id_number)
id_number = 0

请检查如何传回参数,我已经添加了一些!!!!!!!!!在那里显示我的任务。 请检查如何传回参数,我已经添加了一些!!!!!!!!!!在那里显示我的任务。 请检查如何传回参数,我已经添加了一些!!!!!!!!!!在那里显示我的任务。

【问题讨论】:

  • 我不明白您所说的“回传”是什么意思。循环不是函数。请尝试给出一个更具体的示例,显示代码并没有完全按照您的意愿执行,确切解释它的作用,完全您希望它做什么, 确切地这有什么不同。还要确保您的代码正确缩进。如果您在此处解释总体目的,也会有所帮助。我认为you are trying to solve the wrong problem的概率很大。
  • @KarlKnechtel 好的,我会尽快发布我的原始代码

标签: python for-loop


【解决方案1】:

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

while a <= a_end:
 while b <= b_end:
    if condition_b1:
       action_b1
    else:
       action_b2

    for c in (c_start,c_end):
       if condition_c1:
          action_c1
          a += 1
          b += 1
       else: 
          action_c2
          a = new_start
          b = new_start

这就像你的 for 循环一样循环 a 和 b 的值,但是如果条件 c 不满足,你可以跳到 a 和 b 的不同值,然后从那里继续循环。它有点笨拙,但我认为它可以实现您想要的。

【讨论】:

  • 我的任务比较复杂,可以看看我的原码吗? tks.
  • 在这里工作一下?
猜你喜欢
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2017-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多