当列表中的字符串与lis上一个值中的字符串不同时运行函数

2024-10-03 17:19:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,我想每10秒运行一次。它有一个全局变量,它是一个名为marketId的字符串

在某个时刻,这个字符串会改变。当这种情况发生时,我想运行一个新函数

为此,我尝试了以下方法:

def test():
    print("working")


def loop():

    marketId_list = []

    while True:

        time.sleep(10)

        full_function()

        marketId_list.append(marketId)

        for i in marketId_list:

            if marketId_list[i] != marketId_list[i-1]:

                test()

但是当我运行loop()函数时,我得到了一个错误:

TypeError: list indices must be integers or slices, not str

如果能帮上忙,我们将不胜感激,干杯。桑迪


Tags: 方法函数字符串testlooptruedef情况