python-3.x 如何在“列表赋值索引超出范围”错误后从顶部重新启动脚本,循环100次以上,没有问题,然后在出现此错误时停止

prdp8dxp  于 2023-01-27  发布在  Python
关注(0)|答案(1)|浏览(140)

它去真实的列表,每次从列表中删除,最后一个它得到列表回来,并重新开始。我只是一个业余的尝试运行我的交易机器人。它运行了几个小时,循环已经超过1000次,有时这会触发。当我手动重新启动整个脚本的问题消失了。在下,除了我能不能做一个命令,只是重新启动重置整个脚本了吗??或者有没有更好的方法
有什么想法都欢迎

all = ['ETH/EUR', 'BTC/EUR', 'ADA/EUR', 'XRP/EUR', 'ETH/EUR', 'SOL/EUR', 'MANA/EUR',]
symbol = all[0]

def getsignals():
    '''
    # this function checks the symbols or tickers one by one for buying or 
    #selling triggers
    '''

def runbot(): 
    getsignals()
    time.sleep(2)

while True:
    try:
        del all[0]          
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()

        del all[0]
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()

        del all[0]
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()`

        del all[0]
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()

        del all[0]
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()

        del all[0]
        symbol = all[0]
        print(F'-- The new symbol is now -- {symbol}')
        runbot()
        all = ['ETH/EUR', 'BTC/EUR', 'ADA/EUR', 'XRP/EUR', 'ETH/EUR', 'SOL/EUR', 'MANA/EUR',] 
        print('------that was all of them------new round------')

    except Exception as e:
        print(e)
        print('-----error terror-----')
        playsound('problem.wav') 
          
        
    time.sleep(5
jogvjijk

jogvjijk1#

最后我自己找到了一个解决方案,创建了一个主函数,并把整个脚本放在这个函数中,然后在底部添加了另一个while True循环。

def main():
    all = ['ETH/EUR', 'BTC/EUR', 'ADA/EUR', 'XRP/EUR', 'ETH/EUR']
    symbol = all[0]

    def getsignals():
    '''
    # this function checks the symbols or tickers one by one for buying 
    #and/or selling triggers
    '''

    def runbot(): 
        getsignals()
        time.sleep(2)

    while True:
        try:
            del all[0]          
            symbol = all[0]
            print(F'-- The new symbol is now -- {symbol}')
            runbot()

            del all[0]
            symbol = all[0]
            print(F'-- The new symbol is now -- {symbol}')
            runbot()

            del all[0]
            symbol = all[0]
            print(F'-- The new symbol is now -- {symbol}')
            runbot()`

            del all[0]
            symbol = all[0]
            print(F'-- The new symbol is now -- {symbol}')
            runbot()
            all = ['ETH/EUR', 'BTC/EUR', 'ADA/EUR', 'XRP/EUR', 'ETH/EUR'] 
           print('------that was all of them------new round------')

        except Exception as e:
            print(e)
            print('-----error terror-----') 
            main()

while True:
    try:
        main()

    except Exception as e:
        print('still problems')

这样机器人会再次启动,错误会被重置。对我来说很好用。

相关问题