调用另一个函数后重新启动一个函数



我编写了一个Python函数来自动化软件程序。工作流程如下所示:

def main():
while True:
do A
if A is not done correctly for more than 100 times:
reboot()
while True:
do B
if B is not done correctly for more than 100 times:
reboot()
while True:
do C
if C is not done correctly for more than 100 times:
reboot()
def reboot():
restart the software program

我目前遇到的问题是,例如,如果B操作不正确,它将触发重新启动。在执行重新启动后,它将使我回到执行B.的while循环

我真正需要的是脚本在重新启动后始终从A开始。

我已经做了研究,知道Python中没有GoTo,人们建议对这类应用程序使用while循环,但不知何故,我看不出它在我的情况下是如何工作的。如有任何建议,我们将不胜感激,谢谢!

Goto是邪恶的,很容易造成一些大的执行缺陷,这就是为什么它在大多数专业环境中不再使用的原因。

假设你的、A、B或C是某种条件,在这种情况下,你可以创建一个函数来评估该条件,尽管这在很大程度上取决于条件是什么

为什么不根据您所拥有的任何业务逻辑,用适当的条件调用函数呢。这就是我的建议:

def do_the_work(...condition params):
while True:
# do the actual work and evaluate the conditions
# if the conditions don't pass for X times
# then just break this loop and return

while True:
# main business logic here
# will decide how to call the
# do_the_work function
# meaning, deciding what params to send to it

解释是,在脚本末尾定义的main while循环将是您的"main",每次"do_The_work"结束其while循环并返回时,它都会重做工作。

最新更新