Exit Program With the if Condition in Python

Exit Program With the if Condition in Python

Fumbani Banda Oct-22, 2021 Oct-19, 2021 Python Python Input

This tutorial will demonstrate a python exit program that uses if conditions and sys.exit().

Exit Implementation

exit() is intended for interactive shell while sys.exit() is for use in programs.

import sys

while True:
   response = input('Continue?:')
   if response.lower().startswith("y"):
      print("ok,continue")
   elif response.lower().startswith("n"):
      print("Exiting,Bye")
      sys.exit()

Output:

Continue?:yes
ok,continue
Continue?:y
ok,continue
Continue?:no
Exiting,Bye
Fumbani Banda avatar Fumbani Banda avatar

Fumbani is a tech enthusiast. He enjoys writing on Linux and Python as well as contributing to open-source projects.

LinkedIn GitHub

Related Article - Python Input

  • Read User Input as Integers in Python
  • Detect Keypress in Python
  • Python Get User Input With Timeout
  • Take Input of Unspecified Length in Python
  • Simulate Keyboard Inputs in Python
  • Input Multiple Lines in Python