ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 25 Apr 2018 12:37:39 -0500How to end a running script inside a "if" statementhttp://ask.sagemath.org/question/42159/how-to-end-a-running-script-inside-a-if-statement/Hello,
I have a prg.sage script inside which a conditional statement takes place. I would like the script to stop running if the condition is true after having called load("prg.sage") command, and still be in the sage environment. Pure python would allow me to use sys.exit()... What is the best way to do it the "sage way"? Wed, 25 Apr 2018 07:57:55 -0500http://ask.sagemath.org/question/42159/how-to-end-a-running-script-inside-a-if-statement/Answer by nbruin for <p>Hello,</p>
<p>I have a prg.sage script inside which a conditional statement takes place. I would like the script to stop running if the condition is true after having called load("prg.sage") command, and still be in the sage environment. Pure python would allow me to use sys.exit()... What is the best way to do it the "sage way"? </p>
http://ask.sagemath.org/question/42159/how-to-end-a-running-script-inside-a-if-statement/?answer=42165#post-id-42165Since `load("script.sage")` basically has the same effect as executing the code in the file as if typed in at the prompt, I think your question is just a matter of "how do l let a piece of python code end its execution and return control to the point of invocation. So, all regular python flow control structures are at your disposal. `sys.exit()` should work in sage but it doesn't do what you ask: it will end the sage session.
If you need to break out of a single for or while loop, you can use `break`. Python does not provide multi-level breaks, so if you need to break out of more complicated flow control, it's probably easiest to place the entire procedure in a function and use `return`.Wed, 25 Apr 2018 12:37:39 -0500http://ask.sagemath.org/question/42159/how-to-end-a-running-script-inside-a-if-statement/?answer=42165#post-id-42165