Skip to content

Instantly share code, notes, and snippets.

@nick3499
Created July 13, 2023 23:32
Show Gist options
  • Save nick3499/0971b52f808fc984c6f43c36013657ed to your computer and use it in GitHub Desktop.
Save nick3499/0971b52f808fc984c6f43c36013657ed to your computer and use it in GitHub Desktop.
Countdown Timer: time.sleep(), sys.argv()
#!/bin/python3
'''Countdown Timer
`$ python3 countdown.py 15` <-- 15 min timer example'''
from time import sleep
from sys import argv
tot_secs = int(argv[1]) * 60 # convert minute(s) string
def countdown(tot_secs):
'''Countdown total seconds in 5 sec increments format to digital clock format'''
while tot_secs:
mins, secs = divmod(tot_secs, 60) # (2, 0) --> 120 sec / 60 with zero secs remaining (remainder)
print(f'{mins:2d}:{secs:02d}') # 2:00 (convert `mins` and `secs` to digital format)
sleep(5) # pause 5 secs for time sequence
tot_secs -= 5 # subtract 5 from total seconds
print('\x1b[1;7;31m END \x1b[0m') # print END when total seconds reaches zero
countdown(tot_secs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment