简体   繁体   English

python 出错重启循环

[英]Restarting a Loop if an error occurs in python

I am having some trouble with a script which produces an IndexError around 1 out 1000 times its run.我在使用一个脚本时遇到了一些问题,该脚本在运行 1000 次时会产生大约 1 次的 IndexError。

The script is an add on for a piece of software that I cant actually edit or it crashes the external software im using this for.该脚本是我无法实际编辑的软件的附加组件,否则它会使我使用它的外部软件崩溃。

I am calling this script multiple times from my own script is there any way to detect if an error has occurred and loop back to try running the script again?我从我自己的脚本中多次调用此脚本,有什么方法可以检测是否发生错误并循环返回以尝试再次运行脚本? (The script has multiple random elements, this is why the risk to repeated failures is low). (脚本有多个随机元素,这就是重复失败的风险很低的原因)。 I would prefer to go into the add on script and edit it manually but this doesnt appear to be an option我更愿意将 go 添加到脚本中并手动编辑它,但这似乎不是一个选项

I have the following using the PYPI goto statement ( https://pypi.org/project/goto-statement/ ) and using except, however the error doesnt appear to be recognized and I get the same error message as I would get without the goto or except statements.我有以下使用 PYPI goto 语句 ( https://pypi.org/project/goto-statement/ ) 并使用 except,但是错误似乎没有被识别,我得到了与没有goto 或 except 语句。

def function (stuff thats passed into the other script):
    for i in range(5000):
        print(i)
        try:
            runscript (info passed into script) 
        except IndexError:
            print(i,'fails')
            continue

There are many things wrong with this, lets go over it:这有很多问题,让 go 解决它:

from goto import with_goto

You should avoid using goto as it can cause strange code timelines and make it difficult to read or understand your code.您应该避免使用goto ,因为它会导致奇怪的代码时间线,并使您的代码难以阅读或理解。

def function:

You forgot your parenthesis, it should look like def function():你忘了你的括号,它应该看起来像def function():

for i in range(5000):

Replace this with while i < 5000将其替换为 while i < 5000

print(i,'fails')

If you are trying to make an error message, use raise() .如果您试图发出错误消息,请使用raise()

i=i+1

In a python for loop, it automatically increments.在 python for循环中,它会自动递增。

Can you... just... not?你能……只是……不吗?

def func():
  for i in range(5000):
    try:
      runscript
    except IndexError:
      return False
  return True

while not func():
  pass

This will run your function until it manages to do whatever runscript is 5000 times in a row, but it doesn't have any weird bytecode manipulation.这将运行您的runscript直到它设法连续执行 5000 次运行脚本,但它没有任何奇怪的字节码操作。 There are other ways as well:还有其他方法:

successes = 0
while successes < 5000:
  try:
    runscript
    successes += 1
  except IndexError:
    successes = 0

It should be clear that this is a Bad Idea™ based on this quote at the bottom of that page:应该清楚,这是一个基于该页面底部引用的 Bad Idea™:

The idea of goto in Python isn't new. goto in Python 的想法并不新鲜。 There is another module that has been released as April Fool's joke in 2004.还有一个模块是在 2004 年作为愚人节玩笑发布的。

If you're trying to use something that was originally released as an April Fool's joke, there's probably a better way.如果您尝试使用最初作为愚人节玩笑发布的内容,可能有更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM