0

After running this python code in Jupyter Notebook and type o :

p = input("type anything for detecting error:")
if p == "hi":
    print(hellow)
else:
    print('hi')
    exit()
print(p)

it comes out following: type anything for detecting error:o hi o

Then the kernal dye,and notebook pumps out message: 'The kernel appears to have died. It will restart automatically.' It should not print o after i exit the program. enter image description here

and even I don't print anything after exit():

p = input("type anything for detecting error:")
if p == "hi":
    print(hellow)
else:
    print('hi')
    exit()
p == "hi"

it still will pumps out the kernel is dead message. enter image description here

so I think I find bug of exit() function in jupyter notebook,right? Or it just exist in my computer? Can sb help me figure this out,plz.

  • 1
    Why do you need `exit()`? Calling it will [actually stop IPython](https://stackoverflow.com/a/53676195/1814420). – Triet Doan May 19 '21 at 15:14
  • Because I tried to write try and error code and it did't exit and start print code below it: ``` fname = input('Enter the file name: ') try: fhand = open(fname) except: print("file doesn't exist!") quit() count = 0 try: for line in fhand: if line.startswith('Subject:'): count = count + 1 except: quit() print('There were', count, 'subject lines in', fname) ``` but if i type " " it just comes out : Enter the file name: file doesn't exist! There were 0 subject lines in so I simplified the code and ask problem below. – Howard Chen May 19 '21 at 15:35
  • Did my answer help you? If yes, please consider [marking it as an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – Triet Doan May 20 '21 at 10:29

2 Answers2

0

The kernel died not because of your computer, but due to the exit() command. As confirmed by a Jupyter Notebook developer on Github:

At present, using exit() will make the notebook think that the kernel has died, and it will automatically start a new kernel.

If you simply want to stop the execution, wrap your code in a function and use return instead.

def my_func():
    p = input("type anything for detecting error:")
    if p == "hi":
        print(hellow)
    else:
        print('hi')

        # Use return instead of exit()
        return
    print(p)

# Call the function
my_func()
Triet Doan
  • 11,455
  • 8
  • 36
  • 69
  • but Jupiter notebook also do rest of the function after exit(),I wonder why,because if I run it on coursera python code playground, it would not happened. – Howard Chen May 22 '21 at 09:30
0

You can actually exit Python Program without shutting down the kernel with the help of functions. Your code can be written like this,

def main():
    p = input("type anything for detecting error:")
    if p == "hi":
        print(hellow)
    else:
        print('hi')
        return
    print(p)
main()

And also the kernel simply dies if you call exit() while sys.exit() raises an error in IPython. This is the only way you can achieve the desired behaviour.

Shreyan Avigyan
  • 183
  • 1
  • 11