Python Continue or Exit Based on User Input

In this python tutorial, you will learn about the Python exit command with a few examples. Here we will check:

  • Python quit() function
  • Python exit() function
  • Python sys.exit() function
  • Python os.exit() function
  • Python raise SystemExit
  • Program to stop code execution in python
  • Difference between exit() and sys.exit() in python

Python exit command

Let us check out the exit commands in python like quit(), exit(), sys.exit() commands.

Python quit() function

In python, we have an in-built quit() function which is used to exit a python program. When it encounters the quit() function in the system, it terminates the execution of the program completely.

It should not be used in production code and this function should only be used in the interpreter.

Example:

          for val in range(0,5):     if val == 3:         print(quit)         quit()     print(val)        

After writing the above code (python quit() function), Ones you will print " val " then the output will appear as a" 0 1 2 ". Here, if the value of "val" becomes "3" then the program is forced to quit, and it will print the quit message.

You can refer to the below screenshot python quit() function.

Python quit() function
Python quit() function

Python exit() function

We can also use the in-built exit() function in python to exit and come out of the program in python. It should be used in the interpreter only, it is like a synonym of quit() to make python more user-friendly

Example:

          for val in range(0,5):     if val == 3:         print(exit)         exit()     print(val)        

After writing the above code (python exit() function), Ones you will print " val " then the output will appear as a" 0 1 2 ". Here, if the value of "val" becomes "3" then the program is forced to exit, and it will print the exit message too.

You can refer to the below screenshot python exit() function.

Python exit() function
Python exit() function

Python sys.exit() function

In python, sys.exit() is considered good to be used in production code unlike quit() and exit() as sys module is always available. It also contains the in-built function to exit the program and come out of the execution process. The sys.exit() also raises the SystemExit exception.

Example:

          import sys marks = 12 if marks < 20:     sys.exit("Marks is less than 20") else:     print("Marks is not less than 20")        

After writing the above code (python sys.exit() function), the output will appear as a" Marks is less than 20 ". Here, if the marks are less than 20 then it will exit the program as an exception occurred and it will print SystemExit with the argument.

You can refer to the below screenshot python sys.exit() function.

Python sys.exit() function
Python sys.exit() function

Python os.exit() function

So first, we will import os module. Then, the os.exit() method is used to terminate the process with the specified status. We can use this method without flushing buffers or calling any cleanup handlers.

Example:

          import os for i in range(5):     if i == 3:         print(exit)         os._exit(0)     print(i)        

After writing the above code (python os.exit() function), the output will appear as a" 0 1 2 ". Here, it will exit the program, if the value of 'i' equal to 3 then it will print the exit message.

You can refer to the below screenshot python os.exit() function.

Python os.exit() function
Python os.exit() function

Python raise SystemExit

The SystemExit is an exception which is raised, when the program is running needs to be stop.

Example:

          for i in range(8):     if i == 5:         print(exit)         raise SystemExit     print(i)        

After writing the above code (python raise SystemExit), the output will appear as " 0 1 2 3 4 ". Here, we will use this exception to raise an error. If the value of 'i' equal to 5 then, it will exit the program and print the exit message.

You can refer to the below screenshot python raise SystemExit.

Python raise SystemExit
Python raise SystemExit

Program to stop code execution in python

To stop code execution in python first, we have to import the sys object, and then we can call the exit() function to stop the program from running. It is the most reliable way for stopping code execution. We can also pass the string to the Python exit() method.

Example:

          import sys my_list = [] if len(my_list) < 5:   sys.exit('list length is less than 5')        

After writing the above code (program to stop code execution in python), the output will appear as a" list length is less than 5 ". If you want to prevent it from running, if a certain condition is not met then you can stop the execution. Here, the length of "my_list" is less than 5 so it stops the execution.

You can refer to the below screenshot program to stop code execution in python.

python exit command
Program to stop code execution in python

Difference between exit() and sys.exit() in python

  • exit() – If we use exit() in a code and run it in the shell, it shows a message asking whether I want to kill the program or not. The exit() is considered bad to use in production code because it relies on site module.
  • sys.exit() – But sys.exit() is better in this case because it closes the program and doesn't ask. It is considered good to use in production code because the sys module will always be there.

In this Python tutorial, we learned about the python exit command with example and also we have seen how to use it like:

  • Python quit() function
  • Python exit() function
  • Python sys.exit() function
  • Python os.exit() function
  • Python raise SystemExit
  • Program to stop code execution in python
  • Difference between exit() and sys.exit() in python

smithlearothat.blogspot.com

Source: https://pythonguides.com/python-exit-command/

0 Response to "Python Continue or Exit Based on User Input"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel