Feb-24-2021, 05:40 PM
I'm not sure how to use try/except, as Lars60+ suggested. The documentation says sys.exit raises a SystemExit exception and then exits. I want the exit but no further output. I don't understand why the first code exits without any coincident output, which is exactly what I want.
It seems to have worked seamlessly for you deanhystad. bowlofred also asked about the IDE. I'm running Python 3.7 in Spyder 4.2.0. Maybe it's specific to Spyder?
Thanks for the note about the style guidelines. What violated the style guidelines: using a colon to join two lines?
It seems to have worked seamlessly for you deanhystad. bowlofred also asked about the IDE. I'm running Python 3.7 in Spyder 4.2.0. Maybe it's specific to Spyder?
Thanks for the note about the style guidelines. What violated the style guidelines: using a colon to join two lines?
(Feb-24-2021, 05:20 PM)deanhystad Wrote: To exit a program without an error traceback you need to write a program that doesn't raise an exception. Or if it does raise an exception, provide an exception handler (as per Larz60+).
When I run your program like this:
import sys 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 sum_two(3.5, 6)I do not get any mention of using exit or quit. I get this:
...
Output:a and b must both be integers.
My guess is there is something catching the SystemExit exception and generating the messages you are seeing. What are you using for development? I am running Python 3.8.2 on Windows 10. I would stop using sys.exit() (I never use it) and find a better way to handle the problem.
By the way, ou can write your comparison:
if a+b > 15 and a+b < 20: sum = 20like this:
if 15 < a+b < 20: sum = 20And though this is allowed, all style guidelines say you should never use it;
else: sum = a+b