Skip to content

Instantly share code, notes, and snippets.

@PeterHindes
Last active February 15, 2022 18:23
Show Gist options
  • Save PeterHindes/d910142a57910169248490fc3ae0d210 to your computer and use it in GitHub Desktop.
Save PeterHindes/d910142a57910169248490fc3ae0d210 to your computer and use it in GitHub Desktop.
Handle a user wanting to quit a looping terminal program.
import signal
import sys
import time
def signal_handler(signal, frame):
print("Wasting your time...")
time.sleep(3)
print("Goodbye")
raise SystemExit
signal.signal(signal.SIGINT, signal_handler)
print("hello!, this program will check if a year is a leap year")
while True:
while True:
global year
year = input("year: ")
if year.lower() == "q":
signal.raise_signal( signal.SIGINT )
try:
year = int(year)
break
except:
print("Only decimals or the letter q (to quit) are accepted")
#math
leap = (
(year % 4 == 0) and
(
(year % 100 != 0) or
(year % 400 == 0)
)
)
#checks
if leap:
print("year: "+str(year)+" is a leapyear")
else:
print("year: "+str(year)+" is not a leaapyear")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment