【问题标题】:Force a function to exit强制函数退出
【发布时间】:2015-05-28 12:09:08
【问题描述】:

我尝试了从returnsys.exitSystemExit 的所有方法,但都不起作用。

我有一个main 函数,它运行第一个函数;在第一个函数结束时,它运行第二个函数;在第二个函数结束时,它运行第三个函数;之后它会询问我是要再次运行程序还是退出。无论选择如何,它总是让我回到main

有没有办法强制关闭函数内部的函数内部的函数?

编辑: 示例:

def main(RPNlist):
    some code here
    #this function converts a .txt file to a list, wich is a math operation
    y=list
    print(list)
    return(func2(y))
def func2(y):
    some code here
    #this function converts the operation to reverse polish notation
    z=RPNlist
    print(RPNlist)
    return(function3(RPNlist))
def func3(RPNlist):
    some code here
    # this solves the RPN operation
    a=answer
    print(a)
    choice=input("do you want to exit?(Input y to continue or n to exit): ")
    if choice=="y":
        return(main(x))
    elif choice=="n":
        #here's the part where I need to exit

#I think this may be the issue, but is the only way I found for the whole program to run
global RPNlist
RPNlist=[]
main(RPNlist)

【问题讨论】:

  • 请在此处粘贴您的代码
  • 它真的很长...... 425 行...... 这是一个 RPN 计算器
  • @sshashank124 我试过了,还是返回主函数
  • @sshashank124 他确实提到过使用sys.exit
  • 使用调试器单步执行,调用 exit 无论如何都应该退出。永远无法到达退出代码。

标签: python function exit


【解决方案1】:

这真的取决于你的代码。

return value

它总是会退出你的功能。问题不在这里。也许没有调用 return (如果它在 if 中)。也许你有一个循环,在这种情况下,使用 break 退出循环。让应用自行关闭。当没有剩余的方法调用时,应用程序将自行退出。只是摆脱循环。如果它是一个无限循环,您也可以为循环设置一个条件,在其中放入一个布尔值,然后将布尔值设置为 false。

没有你的代码很难提供帮助,如果你想要更好的答案,你需要展示你的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 2012-07-11
    • 2012-09-10
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多