Last active
December 16, 2017 19:13
-
-
Save gkiryaziev/00f8c97e12aaed0edb19e4047da64c7c to your computer and use it in GitHub Desktop.
Countdown timer
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 time | |
import sys | |
import winsound | |
def countdown(t): | |
print() | |
while t: | |
mins, secs = divmod(t, 60) | |
if mins >= 60: | |
hours, mins = divmod(mins, 60) | |
else: | |
hours = 0 | |
timeformat = '{:02d}:{:02d}:{:02d}'.format(hours, mins, secs) | |
print(timeformat, end='\r') | |
time.sleep(1) | |
t -= 1 | |
def usage(): | |
print("\nCountdown timer v0.1, (c)gkiryaziev 2017") | |
print("\nUsage:") | |
print(" python countdown.py [sec.]") | |
print(" countdown.exe [sec.]") | |
sys.exit() | |
def main(t): | |
try: | |
countdown(int(t)) | |
print("Time's up!") | |
for i in range(3): | |
winsound.Beep(1500, 500) | |
time.sleep(0.5) | |
except KeyboardInterrupt: | |
print("Stop and quit.") | |
except ValueError: | |
print("\n\tError: Argv must be an integer value.") | |
except: | |
print(sys.exc_info()[0]) | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
print() | |
data = input("Enter interval [sec.]: ") | |
main(data) | |
elif len(sys.argv) == 2: | |
if sys.argv[1] == "--help": | |
usage() | |
if sys.argv[1] == "--version": | |
print("v0.1") | |
sys.exit() | |
main(sys.argv[1]) | |
else: | |
usage() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment