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?
#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


Messages In This Thread
RE: Basic Coding Question: Exit Program Command? - by snippsat - Nov-19-2023, 05:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Schedule exit a program at a specific time 4 am every day. chubbychub 3 210 May-17-2024, 03:45 PM
Last Post: chubbychub
  python difference between sys.exit and exit() mg24 1 1,919 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  force a program to exit ? Armandito 3 2,626 May-12-2022, 04:03 PM
Last Post: Gribouillis
  [solved] Basic question on list matchiing paul18fr 7 1,956 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  Very basic calculator question BoudewijnFunke 4 2,010 Dec-10-2021, 10:39 AM
Last Post: BoudewijnFunke
  trying to understand a string literal in a basic server program CompleteNewb 4 2,181 Nov-14-2021, 05:33 AM
Last Post: deanhystad
  PLEASE HELP! Basic Python Program joeroffey 5 2,498 Apr-10-2021, 02:59 PM
Last Post: jefsummers
  basic question isinstance tames 5 2,900 Nov-23-2020, 07:20 AM
Last Post: tames
  ERROR: Command errored out with exit status 1 calesii 3 7,250 Oct-23-2020, 05:39 PM
Last Post: snippsat
  basic question about tuples and immutability sudonym3 6 3,001 Oct-18-2020, 05:11 PM
Last Post: sudonym3

Forum Jump:

User Panel Messages

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