python - Exit program without triggering exception handling -
i wasn't sure how title question, issue this; have program set guess number , handle exceptions. program loops until number guessed, when program exits, exception message shows @ same time. how can fix this?
num = none while num != 31: try: num = int(input("what age of creator? \n")) if num < 31: print("higher! guess again! \n") elif num > 31: print("lower! guess again! \n") elif num == 31: print("good guess!") exit() except: print("numbers only! \n") this output:
what age of creator? 31 guess! numbers only!
process finished exit code 0
if want keep exception message, recommend break instead of exit, more natural exit program. try this:
try: ... elif num == 31: print("good guess!") break except valueerror: print("numbers only! \n") also, should catch specific error rather catch-all bare except.
if want silence error messages, should use pass instead. docs:
the
passstatement nothing. can used when statement required syntactically program requires no action.
try: ... except valueerror: pass pass innocuous placeholder statement nothing.
Comments
Post a Comment