6

I am new at coding. I am working on jupyter notebook using anaconda environment. I have this sample code where I am using sys.exit() command. When it gets executed it exits script with several error messages. Here is my sample code.

import sys

name = 'joh'
if name == 'john':
    print('Name matches')
else:
    print('Incorrect name')
    sys.exit(0)

... some more code

Upon execution, it returns

Incorrect name
An exception has occurred, use %tb to see the full traceback.

SystemExit: 0


C:\Users\ankur.kulshrestha\AppData\Local\Continuum\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:2889: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)

How can I gracefully exit the script without receiving all these error messages (apart from the first line which I have actually put in the code.)

KnowledgeSeeeker
  • 620
  • 1
  • 9
  • 14
  • 1
    You should not be exiting at all from the Jupyter Notebook. That's the reason for the message. – darthbith Apr 17 '19 at 19:56
  • But I want to exit script execution. Means if name doesn't match I don't want to execute rest of the following code. – KnowledgeSeeeker Apr 18 '19 at 06:34
  • 1
    Possible duplicate of [IPython Notebook - early exit from cell](https://stackoverflow.com/questions/24005221/ipython-notebook-early-exit-from-cell) – Vlad Iliescu Apr 18 '19 at 06:37
  • Then put that code in a different cell and don't execute it. Or put it in a function and don't call the function. – darthbith Apr 18 '19 at 13:14

0 Answers0