Ask Your Question

How to end a running script inside a "if" statement

asked 2018-04-25 14:57:55 +0100

gerard gravatar image


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"?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2018-04-25 19:37:39 +0100

nbruin gravatar image

Since 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2018-04-25 14:57:55 +0100

Seen: 1,757 times

Last updated: Apr 25 '18