Python Forum
Basic Coding Question: Exit Program Command?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic Coding Question: Exit Program Command?
#1
Could anyone provide feedback on the preferred way to terminate a program?

A Google search suggested that there are 3 basic ways to terminate a program.

The three basic methods from Google are:

1. quit()

2. sys.exit("some sort of typed message here")

3. exit()

Thanks for the help!
Reply
#2
(Nov-19-2023, 04:05 PM)RockBlok Wrote: Could anyone provide feedback on the preferred way to terminate a program?
It's a little vague question,i would say that preferred way is to think about a nice way to exit/terminate of program when write it.
Then many time there is no need to use quit(), exit(), sys.exit() at all,in some cases it may be useful.
A example,so here use a control variable q to exit the loop,also this exit program just bye return out of the function.
def file_deletion():
    file_to_delete = input('File to delete: ')
    print(f'Deleting <{file_to_delete}>\n')
    input('Push enter to retun to menu')

def file_creation():
    pass

def show_menu():
    print ("\nExample menu")
    print ("-----------------")
    print ("1) File deletion")
    print ("2) File creation")
    print ("Q) Exit\n")

def menu():
    while True:
        show_menu()
        choice = input('Enter your choice: ').lower()
        if choice == '1':
            file_deletion()
        elif choice == '2':
            file_creation()
        elif choice == 'q':
            return
        else:
            print(f'Not a correct choice: <{choice}>,try again')

if __name__ == '__main__':
    menu()
Output:
G:\div_code\ur_env λ python meny.py Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: car Not a correct choice: <car>,try again Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: 1 File to delete: foo.txt Deleting <foo.txt> Push enter to retun to menu Example menu ----------------- 1) File deletion 2) File creation Q) Exit Enter your choice: q G:\div_code\ur_env
RockBlok likes this post
Reply
#3
Use sys.exit(), and read the documentation of this function first.
RockBlok likes this post
Reply
#4
quit(), exit() and sys.exit() are the same, they all raise a SystemExit exception.
RockBlok likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python difference between sys.exit and exit() mg24 1 1,852 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  force a program to exit ? Armandito 3 2,544 May-12-2022, 04:03 PM
Last Post: Gribouillis
  [solved] Basic question on list matchiing paul18fr 7 1,874 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 1,953 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  trying to understand a string literal in a basic server program CompleteNewb 4 2,127 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  PLEASE HELP! Basic Python Program joeroffey 5 2,446 Apr-10-2021, 02:59 PM
Last Post: jefsummers
  basic question isinstance tames 5 2,845 Nov-23-2020, 07:20 AM
Last Post: tames
  ERROR: Command errored out with exit status 1 calesii 3 7,193 Oct-23-2020, 05:39 PM
Last Post: snippsat
  basic question about tuples and immutability sudonym3 6 2,917 Oct-18-2020, 05:11 PM
Last Post: sudonym3
  "pip install -e ." returns : ERROR: Command errored out with exit status 1 mostafaPython 3 5,523 Jun-09-2020, 09:28 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020