首页 > 解决方案 > 我正在尝试将不同的参数传递给每个时间表的函数,如下所示,

问题描述

import schedule
import time

arr = ['React', 'Python', 'JS', 'PHP', 'anything']
def add_no(key):
    return print(key, ' got it')

for i, index in enumerate(arr):
    schedule.every(3).seconds.do(add_no, index)
    schedule.run_pending()
    time.sleep(2)
    print(i, ':', index)

我试图将不同的参数传递给每个计划的函数,如下所示:每个指定的时间段我想从 arr 传递一个参数并执行代码,然后完成后,我将第二个传递给函数,所以一直到循环退出,就像每 3 分钟我想将 str 作为 arg 传递给我的函数并执行等等:现在终端显示,

0: React
1: Python
React  got it
2: JS
Python  got it
3: PHP
React  got it
JS  got it   
4: anything

我想要的输出是

0: React
React  got it
1: Python
Python  got it
2: JS
JS  got it
3: PHP
PHP got it
JS  got it   

等等

标签: pythonarraysweb-scrapingparameter-passingschedule

解决方案


也许你想要这样的东西

import schedule
import time


arr = ['React', 'Python', 'JS', 'PHP', 'anything']


def add_no(key):
    return print(key, ' got it')


for i, index in enumerate(arr):
    schedule.every(3).seconds.do(add_no, index)
    schedule.run_pending()
    time.sleep(3)
    print(i, ':', index)
0 : React
React  got it
1 : Python
Python  got it
React  got it
2 : JS
JS  got it
Python  got it
React  got it
3 : PHP
PHP  got it
JS  got it
Python  got it
React  got it
4 : anything

推荐阅读