Last active
February 15, 2022 18:23
-
-
Save PeterHindes/d910142a57910169248490fc3ae0d210 to your computer and use it in GitHub Desktop.
Handle a user wanting to quit a looping terminal program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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