Python Forum
How to programmatically exit without Traceback?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to programmatically exit without Traceback?
#1
This code ends by printing 4:
import sys

for i in range(10):
    if i == 5:
          sys.exit(0)
    print(i)
When I enter sum_two(3.5,6), the following prints "SystemExit" and then "UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)":
print()
print('The function sum_two(a,b) will sum integers a and b.')
print('If sum is between 15 to 20 then output will be 20.')

def sum_two(a,b):
    if type(a) != int or type(b) != int:
        print('a and b must both be integers.')
        sys.exit()
    if a+b > 15 and a+b < 20:
        sum = 20
    else: sum = a+b
    return sum
Why doesn't the latter give me a clean exit like the former? Thanks!
Reply


Messages In This Thread
How to programmatically exit without Traceback? - by Mark17 - Feb-24-2021, 03:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question How create programmatically variables ? SpongeB0B 6 1,668 Aug-19-2023, 05:10 AM
Last Post: SpongeB0B
  How to programmatically stop a program in Jupyter Notebook? Mark17 11 39,087 Feb-12-2023, 01:41 PM
Last Post: jp21in
  Create Excel Line Chart Programmatically dee 3 1,310 Dec-30-2022, 08:44 PM
Last Post: dee
  python difference between sys.exit and exit() mg24 1 2,037 Nov-12-2022, 01:37 PM
Last Post: deanhystad
  Adding Language metadata to a PDF programmatically bhargavi22 0 2,014 Aug-17-2020, 12:53 PM
Last Post: bhargavi22

Forum Jump:

User Panel Messages

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