【问题标题】:Reducing count value to repeat a loop cycle is not working. The for loop in python has an exception handler that has a continue statement减少计数值以重复循环循环不起作用。 python中的for循环有一个异常处理程序,它有一个continue语句
【发布时间】:2023-04-04 09:25:01
【问题描述】:

for i in range(0, 650):
    s = ticket[i]
    try:
        response = resource.get(path='ticket/%s' % s[0]) # Get ticket data from RT server
    except urllib2.URLError, e: # If connection fails
        resource = RTResource(url, user, pwd, CookieAuthenticator) # Reconnect to RT server
        count -= 1 # Count re-connection attempts
        if count < 0:
            print "Connection failed at ticket %s" % s[0]
            print "Got %s tickets out of %s" % {i + 1, len(ticket) + 1}
            wb.save(fname)
            sys.exit(1)
        print 'Trying again...'
        i -= 1
        continue
    count = 10
    ...more code here...

上面的代码执行得很好,但是在抛出异常时会跳过一次迭代。我试图减少 i 的值,然后继续循环,以便在引发异常时,循环将重复 i 的相同值。当 i 的值被跳过时,我会从 RT 服务器丢失一张票。我该如何解决?

【问题讨论】:

    标签:
    python
    exception
    continue